View Full Version : Error 2001: Operation Canceled


xMax
07-09-2008, 11:12 AM
I am getting an Error 2001 here and I can't seem to find what the error is:

Here's the code:

Private Sub cmdLogin_Click()
Dim query As String
Dim login As String
Dim thepassword1 As String
Dim thepassword2 As String
Dim validLogin As String
Dim correctPassword1 As String
Dim correctPassword2 As String
txtLogin.SetFocus
login = txtLogin.Text
txtPassword1.SetFocus
thepassword1 = txtPassword1.Text
txtPassword2.SetFocus
thepassword2 = txtPassword2.Text
txtLogin.SetFocus

validLogin = DLookup("LoginName", "[tblLogin]", "LoginName = " & txtLogin.Text)
MsgBox "validLogin"


If validLogin = login Then
correctPassword1 = DLookup("password1", "[tblLogin]", "LoginName = " & txtLogin.Text)
correctPassword2 = DLookup("password2", "[tblLogin]", "LoginName = " & txtLogin.Text)
If ((thepassword1 = correctPassword1) And (thepassword2 = correctPassword2)) Then
MsgBox "Correct Passwords Entered"
Else
MsgBox "Incorrect Passwords"
End If
Else
MsgBox "Invalid User Name"
End If

End Sub


The highlighted line is the 1st DLookUp line. Please tell me what's wrong.

Max

pbaldy
07-09-2008, 11:15 AM
Drop the .Text for starters. If the data type is text, you'll need a further adjustment, detailed here:

http://www.mvps.org/access/general/gen0018.htm

xMax
07-09-2008, 11:46 AM
Ok I do not receive the Error 2001 but I now receive a Invalid Use of Null Value (if the login doesn't exist, then it should be null, and the same with passwords). What am I doing wrong here?

pbaldy
07-09-2008, 11:55 AM
You've declared validLogin as String, which can not accept the Null that the DLookup will return if it finds no match. Try changing that to Variant.

CoRyu
07-23-2008, 02:22 PM
Try this: Instead of using "LoginName = " & txtLogin.Text in the third parameter of the DLookup sentence, add single quotation marks, just like this:

"LoginName = ' " & txtLogin.Text & "'"

If this doesn't work, try using the full path to your form control (e.g, forms!your_form_name!txtLogin) without the .Text, that will fit perfectly.

boblarson
07-23-2008, 04:50 PM
This would be my preference:

validLogin = Nz(DLookup("LoginName", "tblLogin", "[LoginName]='" & txtLogin & "'"),"")