IsNull始终返回True

时间:2014-05-22 13:02:42

标签: vba access-vba

以下代码中的函数IsNull(char)始终返回true。为什么呢?

StrText从文本字段获取它,值为:

strText = +  96666

给我一​​个问题的特定部分:

 If intCounter = 1 And strChar = "+" Then
            For Count = intCounter + 1 To Len(strText)
                Char = Mid(strText, Count, 1)
                If IsNull(Char) = True Then
                    Count = Count + 1
                End If
                If IsNumeric(Char) And Char <> "0" Then
                    Exit For
                    MsgBox "Problem"
                End If
            Next Count
        End If

完整的功能是:

Public Function NumbersOnly(ByVal strText As String) As Boolean
    Dim intCounter As Integer
    Dim strChar As String
    Dim Count As Integer
    Dim Char As String
    strText = Trim(strText)
    For intCounter = 1 To Len(strText)
        strChar = Mid(strText, intCounter, 1)
        If intCounter = 1 And IsNumeric(strChar) = False And strChar <> "+" Then
            NumbersOnly = False
            Exit Function
        End If
        If intCounter = 1 And strChar = "+" Then
            For Count = intCounter + 1 To Len(strText)
                Char = Mid(strText, Count, 1)
                If IsNull(Char) = True Then
                    Count = Count + 1
                End If
                If IsNumeric(Char) And Char <> "0" Then
                    Exit For
                    MsgBox "Problem"
                End If
            Next Count
        End If
        If intCounter <> 1 And (IsNumeric(strChar) = False And strChar <> " ") Then
            NumbersOnly = False
            Exit Function
        End If
    Next intCounter
    NumbersOnly = True
End Function

1 个答案:

答案 0 :(得分:3)

字符串被初始化为空字符串,而不是null或什么都没有。

考虑以下子程序:

Sub test()
    Dim char As String

    If IsNull(char) Then
        Debug.Print "I can't get here."
    ElseIf char = "" Then
        Debug.Print "HELLO THERE!"
    End If

    char = "sometext"
    If IsNull(char) Then
        Debug.Print "I can't get here."
    ElseIf char = "" Then
        Debug.Print "Now this fails too"
    Else
        Debug.Print "Hello Again."
    End If
End Sub

简而言之,您不想检查您的char变量是否为空,您想检查它是否为空字符串。