By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,647 Members | 1,124 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,647 IT Pros & Developers. It's quick & easy.

Login Code

P: 14
I have a problem; i know its a simple code but find it difficult to contruct the code.

I have two fields username and password. I need some code, when a user clicks the button event in the form it checks against the username and password in the table to see if its correct; if correct it let you login else a message box should show either the username or password is wrong.

Can someone give me a sniplet of the code that i could use.

Thanks
Feb 27 '07 #1
Share this Question
Share on Google+
9 Replies


MMcCarthy
Expert Mod 10K+
P: 14,534
I have a problem; i know its a simple code but find it difficult to contruct the code.

I have two fields username and password. I need some code, when a user clicks the button event in the form it checks against the username and password in the table to see if its correct; if correct it let you login else a message box should show either the username or password is wrong.

Can someone give me a sniplet of the code that i could use.

Thanks
Put the following in the button click event. Assuming textbox on form for username is txtUser and for password is txtPass.

Expand|Select|Wrap|Line Numbers
  1. Dim password As String
  2.  
  3. If Not IsNull(Me.txtUser) Then
  4.   password = DLookup("Password","tblUsers","[UserName]=" & Me.txtUser)
  5.   If Not IsNull(password) Then
  6.     If Not IsNull(Me.txtPass) Then
  7.       If password = Me.txtPass
  8.         'Code to Proceed goes here
  9.       Else
  10.         Msgbox "You have entered an incorrect password, please re-enter", vbOkOnly
  11.         Exit Sub
  12.       End If
  13.     Else
  14.       Msgbox "You must enter a password", vbOkOnly
  15.       Exit Sub
  16.     End If
  17.   Else
  18.     Msgbox "You have entered an incorrect username, please re-enter", vbOkOnly
  19.   End if
  20. Else
  21.   Msgbox "You must enter a user name.", vbOKOnly
  22.   Exit Sub
  23. End If
Mary
Feb 27 '07 #2

P: 14
I inserted this code in the button click event handler but keep on getting a command syntax error on If password = Me.txtPass.
at present my table has three field loginid, username and password. Do i have to specify the table through a select statement.

Private Sub go_Click()
On Error GoTo Err_go_Click
Screen.PreviousControl.SetFocus


Dim password As String

If Not IsNull(Me.txtUser) Then
password = DLookup("Password", "tblUsers", "[UserName]=" & Me.txtUser)
If Not IsNull(password) Then
If Not IsNull(Me.txtPass) Then
If password = Me.txtPass


Else
MsgBox "You have entered an incorrect password, please re-enter", vbOKOnly
Exit Sub
End If
Else
MsgBox "You must enter a password", vbOKOnly
Exit Sub
End If
Else
MsgBox "You have entered an incorrect username, please re-enter", vbOKOnly
End If
Else
MsgBox "You must enter a user name.", vbOKOnly
Exit Sub
End If
End Sub

Put the following in the button click event. Assuming textbox on form for username is txtUser and for password is txtPass.

Expand|Select|Wrap|Line Numbers
  1. Dim password As String
  2.  
  3. If Not IsNull(Me.txtUser) Then
  4.    password = DLookup("Password","tblUsers","[UserName]=" & Me.txtUser)
  5.    If Not IsNull(password) Then
  6.       If Not IsNull(Me.txtPass) Then
  7.          If password = Me.txtPass
  8.             'Code to Proceed goes here
  9.          Else
  10.             Msgbox "You have entered an incorrect password, please re-enter", vbOkOnly
  11.             Exit Sub
  12.          End If
  13.       Else
  14.          Msgbox "You must enter a password", vbOkOnly
  15.          Exit Sub
  16.       End If
  17.    Else
  18.       Msgbox "You have entered an incorrect username, please re-enter", vbOkOnly
  19.    End if
  20. Else
  21.    Msgbox "You must enter a user name.", vbOKOnly
  22.    Exit Sub
  23. End If
  24.  
Mary
Feb 27 '07 #3

MMcCarthy
Expert Mod 10K+
P: 14,534
I was only using these as an example as I didn't know what the textboxes on the form were called. Replace txtUser and txtPass with the name of the textboxes on the login form.

Expand|Select|Wrap|Line Numbers
  1. Private Sub go_Click()
  2. On Error GoTo Err_go_Click
  3.   Screen.PreviousControl.SetFocus
  4. Dim password As String
  5.  
  6. If Not IsNull(Me.txtUser) Then
  7.    password = DLookup("Password", "tblUsers", "[UserName]=" & Me.txtUser)
  8.    If Not IsNull(password) Then
  9.       If Not IsNull(Me.txtPass) Then
  10.          If password = Me.txtPass
  11.  
  12.  
  13.              Else
  14.             MsgBox "You have entered an incorrect password, please re-enter", vbOKOnly
  15.             Exit Sub
  16.          End If
  17.       Else
  18.          MsgBox "You must enter a password", vbOKOnly
  19.          Exit Sub
  20.       End If
  21.    Else
  22.       MsgBox "You have entered an incorrect username, please re-enter", vbOKOnly
  23.    End If
  24. Else
  25.    MsgBox "You must enter a user name.", vbOKOnly
  26.    Exit Sub
  27. End If
  28. End Sub
Feb 27 '07 #4

P: 14
I tried what you showed me, i still get the same error;

"compile error: Syntax Error" on If password = Me.txtPass

Not sure whats the problem. I've named my textboxes as txtUser & txtPass - but still get the error. Try on your one - i'm giving up on this now.

Thanks for your help
Feb 28 '07 #5

MMcCarthy
Expert Mod 10K+
P: 14,534
Add 'Then' to the end of the line

Expand|Select|Wrap|Line Numbers
  1. If password = Me.txtPass Then
Feb 28 '07 #6

P: 14
now i get
run time error '94'
invalid use of null
Feb 28 '07 #7

MMcCarthy
Expert Mod 10K+
P: 14,534
Expand|Select|Wrap|Line Numbers
  1. Private Sub go_Click()
  2. On Error GoTo Err_go_Click
  3.   Screen.PreviousControl.SetFocus
  4. Dim password As String
  5.  
  6. If Not IsNull(Me.txtUser) Then
  7.    password = DLookup("Password", "tblUsers", "[UserName]=" & Me.txtUser)
  8.    If Not IsNull(password) Then
  9.       If Not IsNull(Me.txtPass) Then
  10.          If password = Me.txtPass Then
  11.            '**************** Your code here***********
  12.          Else
  13.             MsgBox "You have entered an incorrect password, please re-enter", vbOKOnly
  14.             Exit Sub
  15.          End If
  16.       Else
  17.          MsgBox "You must enter a password", vbOKOnly
  18.          Exit Sub
  19.       End If
  20.    Else
  21.       MsgBox "You have entered an incorrect username, please re-enter", vbOKOnly
  22.    End If
  23. Else
  24.    MsgBox "You must enter a user name.", vbOKOnly
  25.    Exit Sub
  26. End If
  27. End Sub
All possible nulls should be accounted for in the above code. What line exactly is your code stopping at.

Mary
Feb 28 '07 #8

P: 14
The Null is stopping at:
password = DLookup("Password", "tblUsers", "[UserName]=" & Me.txtUser).

Also if i keep the:

On Error GoTo Err_go_Click
then i get this error: Label not defined.

If i delete that line then the null error comes in.

Thanks for all your help
Feb 28 '07 #9

MMcCarthy
Expert Mod 10K+
P: 14,534
Try changing password variable to a variant as follows:

Expand|Select|Wrap|Line Numbers
  1. Private Sub go_Click()
  2. On Error GoTo Err_go_Click
  3.   Screen.PreviousControl.SetFocus
  4. Dim password As Variant
  5.  
  6. If Not IsNull(Me.txtUser) Then
  7.    password = DLookup("Password", "tblUsers", "[UserName]=" & Me.txtUser)
  8.    If Not IsNull(password) Then
  9.       If Not IsNull(Me.txtPass) Then
  10.          If password = Me.txtPass Then
  11.            '**************** Your code here***********
  12.          Else
  13.             MsgBox "You have entered an incorrect password, please re-enter", vbOKOnly
  14.             Exit Sub
  15.          End If
  16.       Else
  17.          MsgBox "You must enter a password", vbOKOnly
  18.          Exit Sub
  19.       End If
  20.    Else
  21.       MsgBox "You have entered an incorrect username, please re-enter", vbOKOnly
  22.    End If
  23. Else
  24.    MsgBox "You must enter a user name.", vbOKOnly
  25.    Exit Sub
  26. End If
  27. End Sub
Mar 1 '07 #10

Post your reply

Sign in to post your reply or Sign up for a free account.