我收到运行时错误'94':无效使用Null

时间:2019-04-08 20:34:55

标签: ms-access access-vba

我在下面输入我的代码,以查看用户是否创建了一个4位数的密码,如果他们没有填写密码,那么密码将设置为并且为空字符串。没什么大不了的。

我正在下面的if语句中对此进行测试,但它返回的是null异常的无效使用:

Dim U As String
If Me.txtUnlock = Null Then
U = 0
Else
U = Me.txtUnlock
End If
If IsNumeric(U) = False Then
MsgBox "Unlock pin only accepts numbers only.", , "Retail unlock"
Me.txtUnlock = Null
Me.txtUnlock.SetFocus
Exit Sub
End If
If Len(U) <> 4 Then
MsgBox "Retail unlock must be four digits long.", , "Retail unlock"
Me.txtUnlock.SetFocus
Exit Sub
End If

然后我检查下面的图钉是否为空:

 If IsNull(Me.txtUnlock) Then
    check = check + 1
    UL = ""
    Else
    UL = Me.txtUnlock
    End If

2 个答案:

答案 0 :(得分:1)

If Me.txtUnlock = Null Then

那你做不到。尝试:

If IsNull(Me!txtUnlock.Value) Then

答案 1 :(得分:0)

使用NZ函数来考虑以下简化代码:

Dim U As String
U = NZ(Me.txtUnlock,0)

Dim fail as Boolean
If Not IsNumeric(U) Then
    MsgBox "Unlock pin only accepts numbers only.", , "Retail unlock"
    fail = true
ElseIf Len(U) <> 4 Then
    MsgBox "Retail unlock must be four digits long.", , "Retail unlock"
    fail = true
End If

If fail then
    With Me.txtUnlock 
        .value = vbNullString
        .SetFocus
    End With
End If