Use Dlookup to Validate User

bconner

Registered User.
Local time
Today, 10:17
Joined
Dec 22, 2008
Messages
183
I am trying to use the code below to validate a User when they log into the database but I am getting RunTime Error 2001.



Code:
Private Sub ButtonLogin_Click()
Dim strUserName As String
Dim strPassword As String
Dim strUserNamePassword As String
 
'Combine User Name and Password entered into Login Form
 
strUserNamePassword = txtUserName & txtPassword
 
'Search for User Name and Password entered from the Login Form in the Tbl_Representatives
 
strUserName = Nz(DLookup("User_Login", "Tbl_Representatives", "User_Login= " & Forms![Frm_Login]!txtUserName), 0)
strPassword = Nz(DLookup("User_Password", "Tbl_Representatives", "User_Password= " & Forms![Frm_Login]!txtPassword), 0)
 
'Validate the User Name and Password exists in the Tbl_Representatives
 
If strUserNamePassword = (strUserName & strPassword) Then
  DoCmd.OpenForm "Frm_Main"
  DoCmd.Close acForm, "Frm_Login"
Else
  lblLoginFailure.Visible = True
 
You need to surround your string values with quotes:
Code:
strUserName = Nz(DLookup("User_Login", "Tbl_Representatives", "User_Login= '" & Me.txtUserName & "'"), "")
strPassword = Nz(DLookup("User_Password", "Tbl_Representatives", "User_Password= '" & Me.txtPassword & "'"), "")
There is no reason to reference through the Forms collection in this case so I changes it. I also changes the value returned if Null to a string instead of a number.
 
Rural Guy thank you very much that worked like a charm!
 

Users who are viewing this thread

Back
Top Bottom