PDA

View Full Version : [SOLVED:] Issues with Dlookup



Deso
10-13-2010, 02:27 AM
I've been trying to write a very simple username/password check using a database for the username and password fields in Access 2003. I'm having various issues with the syntax of Dlookup. The following script, which is based almost entirely on the example in the help file, gives me 'Error 2001; you cancelled the previous operation'.



Private Sub Command12_Click()
Dim Key1As String
Dim Key2 As String
Dim TestVariant As Variant
If IsNull(Me.Text8.Value) Or IsNull(Me.Text10.Value) Then
MsgBox "Please enter a valid username or password."
Else
Key1 = Me.Text8.Value
Key2 = Me.Text10.Value
TestVariant = DLookup("[pwd]", "UserPass", "[usnm] =" & Key1)
MsgBox Variant
End If
End Sub


Can anyone help me eradicate this error? Thanks :)

Imdabaum
10-13-2010, 06:39 AM
I've been trying to write a very simple username/password check using a database for the username and password fields in Access 2003. I'm having various issues with the syntax of Dlookup. The following script, which is based almost entirely on the example in the help file, gives me 'Error 2001; you cancelled the previous operation'.
Can anyone help me eradicate this error? Thanks :)


Try this. I'm assuming you're going to rename the button to something more meaningful after you get the code working, so I won't mention anything about that here.:whistle:

Despite what the VBA help file says, if you use a string variable in your DLookup functions, you need to force the string behavior with single quotes.



Private Sub Command12_Click()
Dim Key1As String
Dim Key2 As String
Dim TestVariant As Variant
If IsNull(Me.Text8.Value) Or IsNull(Me.Text10.Value) Then
MsgBox "Please enter a valid username or password."
Else
Key1 = Me.Text8.Value
Key2 = Me.Text10.Value
TestVariant = DLookup("[pwd]", "UserPass", "[usnm] ='" & Key1 & "'")
MsgBox Variant
End If
End Sub

hansup
10-14-2010, 07:52 AM
You have this line in your Else block:


MsgBox Variant

I tested a similar line here, and it causes a compile error. I think what you intended was this:

MsgBox TestVariant
Including Option Explicit in the declarations section of your module will help you avoid this type of error by complaining about variables you haven't declared.

Imdabaum
10-14-2010, 08:03 AM
Yeah.. that's a good point too.:bow:

hansup
10-14-2010, 08:33 AM
Thanks. I didn't notice that the first time through. Then your response included the VBA tags, and the syntax coloring made it fairly jump out at me. So I think Deso's problem may consist of 2 parts: the data type for usnm which you addressed; and the use of a VBA keyword as the name of an undeclared variable. Kind of a collaborative effort here, thanks to you. :friends:

Imdabaum
10-14-2010, 12:07 PM
I always was better at pair coding.