Solved DLookup Statement Problem (1 Viewer)

Pac-Man

Active member
Local time
Today, 15:34
Joined
Apr 14, 2020
Messages
415
Hello,

Please help me. What is wrong with these statements. Every time it gives type mismatch error. In tblUser, Username is a short text field and lngUserID is a public variable defined as long integer.
Code:
Dim strUserName As String
strUserName = Nz(DLookup("UserName", "tblUser", "UserID ='" & lngUserID & "'"), "Untitled")
 

June7

AWF VIP
Local time
Today, 02:34
Joined
Mar 9, 2014
Messages
5,466
If UserID is a number field, don't use apostrophe delimiters.
 

Pac-Man

Active member
Local time
Today, 15:34
Joined
Apr 14, 2020
Messages
415
If UserID is a number field, don't use apostrophe delimiters.
I tried this:
Code:
Dim strUserName As String
Me.txtUserNameWelcome = lngUserID
strUserName = Nz(DLookup("UserName", "tblUser", "UserID = lngUserID"), "Untitled")

and it gives me new error i.e. The expression you have entered as a query parameter produced this error: 'lngUserID'

To check value of lngUserID I used Me.txtUserNameWelocme = lngUserID to check if correct value is stored in the lngUserID in break mode and running line by line code using F8 key. Correct value is stored in the variable. When I used the same numerical value in DLookup, it works and when I use global variable lngUserID, it don't even though lngUserID contains the same numerical value. lngUserID is dimensioned as long.
 

bob fitz

AWF VIP
Local time
Today, 11:34
Joined
May 23, 2011
Messages
4,719
Try
Code:
strUserName = Nz(DLookup("UserName", "tblUser", "UserID = " & lngUserID), "Untitled")
 

Users who are viewing this thread

Top Bottom