Check if the user is present in the table

aman

Registered User.
Local time
Today, 14:48
Joined
Oct 16, 2008
Messages
1,251
Hi guys

I want to check if the current logging user exist in the tbluser or not. If it doesn't then message should appear that "The user doesn't exist in the database, Please raise request to your team leader to add you in". But if exist then it should check the "status" of the user in tbluser. If status is Admin then all buttons should become visible otherwise just few options should be visible.

But for now in the else section I have just put the message "exist". But this doesn't work. As it displays message "exist" even the current user is not present in the table.

Code:
Private Sub Form_Load()
If DLookup("user", "tbluser", "User='" & Environ("username") & "'") = 0 Then
MsgBox "The user doesn't exist"
Else
MsgBox "exist"
End If
End Sub

Any help would be much appreciated.

Thanks
 
If the dlookup doesn't find a match, the answer will not be 0, I think it'll be null

Therefore, change the second line of your code to

Code:
If isnull(DLookup("user", "tbluser", "User='" & Environ("username") & "'")) Then
 
If the dlookup doesn't find a match, the answer will not be 0, I think it'll be null

Therefore, change the second line of your code to

Code:
If isnull(DLookup("user", "tbluser", "User='" & Environ("username") & "'")) Then
No Peter it should be:

If Nz(DLookup("user", "tbluser", "User='" & Environ("username") & "'"), vbNullString) = vbNullString Then

A Dlookup returning Null will generate an error before the IsNull function kicks in.
 
Last edited:
To avoid Null issues, you could use

If DCount("user", "tbluser", "User='" & Environ("username") & "'") = 0 Then
...
..
 
To avoid Null issues, you could use

If DCount("user", "tbluser", "User='" & Environ("username") & "'") = 0 Then
...
..
Yes, a good solution.

it could also be written:

If DCount("*", "tbluser", "User='" & Environ("username") & "'") = 0 Then
 

Users who are viewing this thread

Back
Top Bottom