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

问题描述 投票:0回答:2

我在下面有我的代码,以查看用户是否创建了一个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
ms-access access-vba
2个回答
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

1
投票
If Me.txtUnlock = Null Then

你不能这样做。试试:

If IsNull(Me!txtUnlock.Value) Then
© www.soinside.com 2019 - 2024. All rights reserved.