Invalid use of Null when using DLookup in MS Access - vba

I want to set the user login access in MS Access which means that if the user logs in as the admin it will show a different form.
I have tried to get the userlevel which is a string and will show things like "Admin" or "User" but it indicated:
Invalid use of Null
At this line:
UserLevel = DLookup("UserSecurity", "tblUser", "[UserLogin] = ' " & Me.txtLoginID.Value & "'")
Here is the full code:
Private Sub Command1_Click()
Dim UserLevel As String 'get the dlookup value
Dim TempPass As String
If IsNull(Me.txtLoginID) Then
MsgBox "Please Enter Login ID", vbInformation, "Login Id Required"
Me.txtLoginID.SetFocus
ElseIf IsNull(Me.txtLoginPass) Then
MsgBox "Please Enter Password", vbInformation, "Login password Required"
Me.txtLoginPass.SetFocus
Else
'process the job
If (IsNull(DLookup("UserLogin", "tblUser", "UserLogin ='" & Me.txtLoginID.Value & "'"))) Or _
(IsNull(DLookup("password", "tblUser", "Password = '" & Me.txtLoginPass.Value & "'"))) Then
MsgBox "Incorrect Password"
Else
TempPass = DLookup("password", "tblUser", "UserLogin = '" & Me.txtLoginID.Value & "'")
UserLevel = DLookup("UserSecurity", "tblUser", "[UserLogin] = ' " & Me.txtLoginID.Value & "'")
'get the usersecurity whcih indicate he is admin or user
DoCmd.Close
If UserLevel = "Admin" Then 'if admin then open employee form else open customer form
'MsgBox "Login ID and password correct "
DoCmd.OpenForm "Employee"
Else
DoCmd.OpenForm "CustomerForm"
End If
End If
End If
End Sub
I have tried to use Nz() but it gives me a null value which takes me to the customer form.

To explain the error that you are receiving: this arises when you attempt to assign a Null value to a variable whose data type is not a Variant, per the MS documentation:
A Variant is a special data type that can contain any kind of data [...] A Variant can also contain the special values Empty, Error, Nothing, and Null.
This error arises in your code because the DLookup function will return Null when no records in the domain fulfill the supplied criteria argument, and can be boiled down to the following two lines:
Dim UserLevel As String
UserLevel = DLookup("UserSecurity", "tblUser", "[UserLogin] = ' " & Me.txtLoginID.Value & "'")
I suspect that this is caused by the leading space in your criteria argument:
"[UserLogin] = ' " & Me.txtLoginID.Value & "'"
^--------------------------------- HERE
Which should probably be:
"[UserLogin] = '" & Me.txtLoginID.Value & "'"
However, you may still wish to account for the case in which no records meet the criteria, which can be accomplished in several ways.
You could use the Nz function and then test for an empty string, e.g.:
UserLevel = Nz(DLookup("UserSecurity", "tblUser", "[UserLogin] = '" & Me.txtLoginID.Value & "'"), "")
Select Case UserLevel
Case "Admin": DoCmd.OpenForm "Employee"
Case "User" : DoCmd.OpenForm "CustomerForm"
Case Else : MsgBox "Invalid UserSecurity Value"
End Select
Or, you could define the UserLevel variable as a Variant (hence permitting a Null value), and test whether such variable is Null using the IsNull function:
Dim UserLevel As Variant ' Or just "Dim UserLevel" since Variant is the default type
UserLevel = DLookup("UserSecurity", "tblUser", "[UserLogin] = '" & Me.txtLoginID.Value & "'")
If IsNull(UserLevel) Then
MsgBox "Invalid UserSecurity Value"
ElseIf UserLevel = "Admin" Then
DoCmd.OpenForm "Employee"
Else
DoCmd.OpenForm "CustomerForm"
End If

Remove the space you have inserted in your criteria, so:
UserLevel = DLookup("UserSecurity", "tblUser", "[UserLogin] = '" & Me.txtLoginID.Value & "'")

Related

VBA in Access - Duplicate Entries Msg Box

Can anyone help me identify what's wrong with this code? If I change DOB in the table to short text and DIM it as a string, the code works. But, with DOB as a date field, DIM as date, I am getting this error message.
"Run-time error '3464'
Data Type mismatch in criteria expression"
When I click on debug, the line bolded below is highlighted in yellow.
Private Sub DOB_AfterUpdate()
Dim DOB As Date
Dim FirstName As String
Dim LastName As String
Dim stLinkCriteria As String
Dim PIN As Integer
'Assign the entered customer name and address to a variable
NewJII = Me.FirstName.Value
NewJII2 = Me.LastName.Value
NewDOB = Me.DOB.Value
stLinkCriteria = "[FirstName] = " & "'" & NewJII & "' and [LastName] = " & "'" & NewJII2 & "' And [DOB] = " & "'" & NewDOB & "'"
**If Me.FirstName & Me.LastName & Me.DOB = DLookup("[FirstName]",
"TblPersonLog", stLinkCriteria) Then**
MsgBox "This Customer, " & stLinkCriteria & " has already been entered in database." _
& vbCr & vbCr & "with DOB " & NewDOB & "" _
& vbCr & vbCr & "Please check Customer name and Date Of Birth again.", vbInformation, "Duplicate information"
Cancel = True
End If
End Sub
Really appreciate any guidance!! I've been trying to troubleshoot this for days. Not very strong in VBA.
First, you can't use DLookup that way. Second, if you wish to cancel, use the BeforeUpdate event. Third, as stated by Tim, use the correct syntax for the date criteria.
Thus, it could be something like this:
Private Sub DOB_BeforeUpdate(Cancel As Integer)
Dim FirstName As String
Dim LastName As String
Dim NewDOB As Date
Dim stLinkCriteria As String
If Not IsNull(Me!DOB.Value) Then
' Assign the entered customer name and DOB to variables.
FirstName = Nz(Me!FirstName.Value)
LastName Nz(Me!LastName.Value)
NewDOB = Me!DOB.Value
stLinkCriteria = "[FirstName] = '" & FirstName & "' And [LastName] = '" & LastName & "' And [DOB] = #" & Format(NewDOB, "yyyy\/mm\/dd") & "#"
Cancel = Not IsNull(DLookup("[FirstName]", "TblPersonLog", stLinkCriteria))
If Cancel = True Then
MsgBox "This Customer, " & FirstName & " " & LastName & ", has already been entered in the database" _
& vbCr & vbCr & "with DOB " & NewDOB & "." _
& vbCr & vbCr & "Please check Customer name and Date Of Birth again.", vbInformation, "Duplicate information"
End If
End If
End Sub

Set the Caption of label to Username

I'm making an Access database and have a secure logon feature. I want to use the Username that is entered in a Label or other control as a "Sign in as " feature.
How do I reference the other form that is not my Sign in form?
I want to attach the ID or User name to Label 66 in form UserViewForm.
Private Sub Command1_Click()
Dim User As String
Dim UserLevel As Integer
Dim TempPass As String
Dim ID As Integer
Dim UserName As String
Dim TempID As String
If IsNull(Me.TxtUserName) Then
MsgBox "Please enter UserName", vbInformation, "Username required"
Me.TxtUserName.SetFocus
ElseIf IsNull(Me.TxtPassword) Then
MsgBox "Please enter Password", vbInformation, "Password required"
Me.TxtPassword.SetFocus
Else
If (IsNull(DLookup("UserLogin", "tblUser", "UserLogin = '" & Me.TxtUserName.Value & "' And UserPassword = '" & Me.TxtPassword.Value & "'"))) Then
MsgBox "Invalid Username or Password!"
Else
TempID = Me.TxtUserName.Value
UserName = DLookup("[UserName]", "tblUser", "[UserLogin] = '" & Me.TxtUserName.Value & "'")
UserLevel = DLookup("[UserType]", "tblUser", "[UserLogin] = '" & Me.TxtUserName.Value & "'")
TempPass = DLookup("[UserPassword]", "tblUser", "[UserLogin] = '" & Me.TxtUserName.Value & "'")
UserLogin = DLookup("[UserLogin]", "tblUser", "[UserLogin] = '" & Me.TxtUserName.Value & "'")
DoCmd.Close
If (TempPass = "password") Then
MsgBox "Please change Password", vbInformation, "New password required"
DoCmd.OpenForm "frmUserinfo", , , "[UserLogin] = " & UserLogin
Else
'open different form according to user level
If UserLevel = 1 Then ' for admin
DoCmd.OpenForm "UserViewForm"
'Else
'DoCmd.OpenForm "Ne"
End If
End If
End If
End If
Forms!UserViewFrom.Label66 = TempID
End Sub
This is for user experience.
For labels, you need to set the Caption property.
Forms!UserViewFrom.Label66.Caption = TempID
Also note that you shouldn't store passwords in plaintext, nor use unsanitized user input to build SQL commands - look up SQL injection.

Controlling Forms based on log in information

I'm been teaching myself Microsoft VBA for Applications and I reached a spot where I really need help getting over a hurdle. I have a login form that controls access by opening a specific form based on assigned roles. Everything works, but it allows a person to see all of the members records. I'm trying to restrict the "Form" to the user who's logged in. Below is the code that I have for logging in.
Private Sub Command1_Click()
Dim User As String
Dim UserLevel As Integer
Dim TempPass As String
Dim ID As Integer
Dim Username As String
Dim TempID As String
If IsNull(Me.txtUserName) Then
MsgBox "Please enter UserName", vbInformation, "Username required"
Me.txtUserName.SetFocus
ElseIf IsNull(Me.txtPassword) Then
MsgBox "Please enter Password", vbInformation, "Password required"
Me.txtPassword.SetFocus
Else
If (IsNull(DLookup("UserLogin", "Users", "UserLogin = '" & Me.txtUserName.Value & "' And UserPassword = '" & Me.txtPassword.Value & "'"))) Then
MsgBox "Invalid Username or Password!"
Else
TempID = Me.txtUername.Value
Username = DLookup("[UserName]", "Users", "[UserLogin] = '" & Me.txtUserName.Value & "'")
UserLevel = DLookup("[UserType]", "Users", "[UserLogin] = '" & Me.txtUserName.Value & "'")
TempPass = DLookup("[UserPassword]", "Users", "[UserLogin] = '" & Me.txtUserName.Value & "'")
UserLogin = DLookup("[UserLogin]", "Users", "[UserLogin] = '" & Me.txtUserName.Value & "'")
DoCmd.Close
If (TempPass = "password") Then
MsgBox "Please change Password", vbInformation, "New password required"
DoCmd.OpenForm "frmUserinfo", , , "[UserLogin] = " & UserLogin
Else
'Open different form according to user level
If userLevel = 1 Then 'For Admin
DoCmd.OpenForm "Training"
Else
'For Trainee
DoCmd.OpenForm "Form", acNormal, , "ID = & TempID 'TempID finds the lastname from UserLogin and I need the number from UserName
End If
End If
End If
End Sub
The "Form" that opens has a Combobox that controls which records show. How do I get the logged in user information to pass to the "Form" based on UserName? if that's even the correct way! Thank you all for you help in advance.

How to fix Compile error creating the Login form in Access

I am creating the from for my ms access database and I am running into a compile error when where "Me.txtUserName is not found.
This is for the Access database.
Dim User As String
Dim UserLevel As Integer
Dim TempPass As String
Dim ID As Integer
Dim UserName As String
Dim TempID As String
If IsNull(Me.txtUserName) Then
MsgBox "Please enter UserName", vbInformation, "Username required"
Me.txtUserName.SetFocus
ElseIf IsNull(Me.txtPassword) Then
MsgBox "Please enter Password", vbInformation, "Password required"
Me.txtPassword.SetFocus
Else
If (IsNull(DLookup("UserLogin", "tblUser", "UserLogin = '" & Me.txtUserName.Value & "' And UserPassword = '" & Me.txtPassword.Value & "'"))) Then
MsgBox "Invalid Username or Password!"
Else
TempID = Me.txtUserName.Value
UserName = DLookup("[UserName]", "tblUser", "[UserLogin] = '" & Me.txtUserName.Value & "'")
UserLevel = DLookup("[UserType]", "tblUser", "[UserLogin] = '" & Me.txtUserName.Value & "'")
TempPass = DLookup("[UserPassword]", "tblUser", "[UserLogin] = '" & Me.txtUserName.Value & "'")
UserLogin = DLookup("[UserLogin]", "tblUser", "[UserLogin] = '" & Me.txtUserName.Value & "'")
DoCmd.Close
If (TempPass = "password") Then
MsgBox "Please change Password", vbInformation, "New password required"
DoCmd.OpenForm "frmUserinfo", , , "[UserLogin] = " & UserLogin
Else
'open different form according to user level
If UserLevel = 1 Then ' for admin
DoCmd.OpenForm "Admin Form"
Else
DoCmd.OpenForm "Navigation Form"
End If
End If
End If
End If
One immediate issue is that you have two variable assignments on a single line here:
UserName = DLookup("[UserName]", "tblUser", "[UserLogin] = '" & Me.txtUserName.Value & "'") UserLevel = DLookup("[UserType]", "tblUser", "[UserLogin] = '" & Me.txtUserName.Value & "'")
These should be moved onto separate lines, or alternatively, the two variable assignments should be separated with a semi-colon.

VBA code to pass parameter of ms-access form to query

I can't seem to figure out a good way to code this into my VBA macro. I'm using Access. I have a form that's serving as a 'login' it Dlookups to make sure the user is within a user table. Once it's confirmed that the user is within the user table, I want it to run a query on another table with the criteria of the username.
This is the criteria of my username field on my query referenced table:
[Forms]![LoginForm].[txtUserName]
This is the Macro of my login form click button
Private Sub btnLogin_Click()
Dim User As String
Dim UserLevel As Integer
Dim TempPass As String
Dim ID As Integer
Dim UserName As String
Dim TempID As String
If IsNull(Me.txtUserName) Then
MsgBox "Please enter UserName", vbInformation, "Username required"
Me.txtUserName.SetFocus
ElseIf IsNull(Me.txtPassword) Then
MsgBox "Please enter Password", vbInformation, "Password required"
Me.txtPassword.SetFocus
Else
If (IsNull(DLookup("UserLogin", "tblUser", "UserLogin = '" & Me.txtUserName.Value & "' And UserPassword = '" & Me.txtPassword.Value & "'"))) Then
MsgBox "Invalid Username or Password!"
Else
TempID = Me.txtUserName.Value
UserName = DLookup("[UserName]", "tblUser", "[UserLogin] = '" & Me.txtUserName.Value & "'")
UserLevel = DLookup("[UserSecurity]", "tblUser", "[UserLogin] = '" & Me.txtUserName.Value & "'")
TempPass = DLookup("[UserPassword]", "tblUser", "[UserLogin] = '" & Me.txtUserName.Value & "'")
UserLogin = DLookup("[UserLogin]", "tblUser", "[UserLogin] = '" & Me.txtUserName.Value & "'")
DoCmd.Close
If (TempPass = "password") Then
MsgBox "Please change Password", vbInformation, "New password required"
DoCmd.OpenForm "frmUserinfo", , , "[UserLogin] = " & UserLogin
Else
'open different form according to user level
DoCmd.OpenQuery "ClockDefectRun", , acReadOnly
End If
End If
End If
End Sub
Whenever I run this, My query opens, but it asks for the field as if it doesn't exist in my forms. How can I pass this programmatically?