Reputation: 375
The function IsNull(char)
in my below code is always returning true. Why?
StrText is getting it from a text field and the value is:
strText = + 96666
The particular section that is giving me an issue:
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
the complete function is:
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
Upvotes: 0
Views: 360
Reputation: 12728
Strings are initialized to an empty string, not null or nothing.
Consider the following subroutine:
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
In short, you don't want to check to see if your char
variable is null, you want to check to see if it's an empty string.
Upvotes: 3