在 MS Access 中使用 DLookup 时无效使用 Null

Invalid use of Null when using DLookup in MS Access

我想在 MS Access 中设置用户登录权限,这意味着如果用户以管理员身份登录,它将显示不同的表单。

我试图获取 userlevel,它是一个字符串,将显示 "Admin""User" 之类的内容,但它指示:

Invalid use of Null

在这一行:

UserLevel = DLookup("UserSecurity", "tblUser", "[UserLogin] = ' " & Me.txtLoginID.Value & "'")

完整代码如下:

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

我曾尝试使用 Nz(),但它给了我一个空值,将我带到了客户表单。

删除您在条件中插入的 space,因此:

UserLevel = DLookup("UserSecurity", "tblUser", "[UserLogin] = '" & Me.txtLoginID.Value & "'")

解释您收到的错误:当您尝试将 Null 值分配给数据类型为 not a [=18 的变量时,就会出现这种情况=],根据 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.

您的代码中会出现此错误,因为当域中没有记录满足提供的条件参数时,DLookup 函数将 return Null,并且可以归结为以下内容两行:

Dim UserLevel As String
UserLevel = DLookup("UserSecurity", "tblUser", "[UserLogin] = ' " & Me.txtLoginID.Value & "'")

我怀疑这是由您的标准参数中的前导 space 引起的:

"[UserLogin] = ' " & Me.txtLoginID.Value & "'"
                ^--------------------------------- HERE

大概应该是:

"[UserLogin] = '" & Me.txtLoginID.Value & "'"

但是,您可能仍希望考虑没有记录符合条件的情况,这可以通过多种方式实现。

您可以使用 Nz 函数,然后测试空字符串,例如:

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

或者,您可以将 UserLevel 变量定义为 Variant(因此允许 Null 值),并使用 IsNull函数:

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