Have Two Text Boxes one for "user name" and the other for "password" and also have a MS Access database with three coloums "UserIdNum" which is an auto num, "UserName" and "password" i want it so when i click one button "create user" it adds the data from the two textboxes to the two coloums "UserName and Password". At the min got it working so it adds the "User name" textbox to the "UserName Coloum" but cant find soloution for password. Heres the coding ive done:

Dim AdUsr As String
AdUsr = UsrAdd.Text

AdUsr = AdUsr.Replace("'", "''")

Dim CommandUser As New OleDbCommand("INSERT INTO UserLoginDetails(UserLogin) values(" + AdUsr + ")", CrtUsrDb)


CrtUsrDb.Open()
Dim LoginRecordsInsertUser As Integer

LoginRecordsInsertUser = CommandUser.ExecuteNonQuery()

CrtUsrDb.Close()

Thanks if you can help :D

Recommended Answers

All 4 Replies

Add the user name and the password at the same time:

Dim AdUsr As String
Dim AdPW As String

AdUsr = UsrAdd.Text
AdPW = PasswordAdd.Text ' Replace with correct textbox name

AdUsr = AdUsr.Replace("'", "''")
AdPW = AdPW.Replace("'", "''") ' If you allow apostrophe in the password

Dim CommandUser As New OleDbCommand("INSERT INTO UserLoginDetails(UserLogin, Password) values('" + AdUsr + "','" + AdPW + "')", CrtUsrDb) ' I added apostrophes because AdUsr and AdPW are strings

and the rest of the code is ok.

One small point: it could be possible to have two users with the same user name and the password but with different UserIdNum...

HTH

Thanks for the help. Still got a problem though when i go to debug i get a sytax error appear "Syntax error in INSERT INTO statement.".

I taken note of your point and put some code in to check this before the rest of the code is ran.

In MS access database, the word Password is reserved as a key word and if you attempt to use it for a column name it will results into errors, especially when you want to update or edit your records into a database.So if you want to specify a name for the password column, never use the word Password, you may either use the world like User_Password, or uPassword, but never use the word Password as it is. Change your column for Password into User_Password as it shows below.

I think this will work for you, try it and if you will get the same problem please reply back again, I will show you an alternative way for adding records into a database....ok try it.

Dim AdUsr As String
    Dim AdPW As String
     
    AdUsr = UsrAdd.Text
    AdPW = PasswordAdd.Text ' Replace with correct textbox name
     
    AdUsr = AdUsr.Replace("'", "''")
    AdPW = AdPW.Replace("'", "''") ' If you allow apostrophe in the password
     
    Dim CommandUser As New OleDbCommand("INSERT INTO UserLoginDetails(UserLogin, User_Password) values('" + AdUsr + "','" + AdPW + "')", CrtUsrDb) ' I added apostrophes because AdUsr and AdPW are strings

Thanks for the reply done what you said and works great :) thanks :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.