rinkydink
asked on
Problem trying to verify characters as Integers
Hi - I'm trying to create a procedure that will verify that the characters typed in a text box are integers, I can't figure out how to verify each character one by one. I'm guessing a for...next loop will work but am unsure of the code. Any advice will be appreciated.
Thanks.
Here is what I got so far:
Dim AnyString As String
AnyString = Text1
Dim x As Integer
x = Len(AnyString)
If x > 10 Then MsgBox "You entered too many numbers": Exit Sub
If x < 10 Then MsgBox "You entered too few numbers": Exit Sub
'For i = 1 To x
'?????
'Next i
Thanks.
Here is what I got so far:
Dim AnyString As String
AnyString = Text1
Dim x As Integer
x = Len(AnyString)
If x > 10 Then MsgBox "You entered too many numbers": Exit Sub
If x < 10 Then MsgBox "You entered too few numbers": Exit Sub
'For i = 1 To x
'?????
'Next i
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
sethi,
there are a problem...
15,14 is numeric and 32767>= 15,14>= -32767
and view his code he want a 10 characters long
may be a long number no a integer
can it be zeros by the left?
x = Len(AnyString) or x = Len(cst(val(AnyString)))
if IsNumeric(txtTextbox)=True then
if round(val(txtTextbox),0)=t xtTextbox then
If x > 10 Then MsgBox "You entered too many numbers": Exit Sub
If x < 10 Then MsgBox "You entered too few numbers": Exit Sub
else
MsgBox "You entered decimals values"
end if
end if
there are a problem...
15,14 is numeric and 32767>= 15,14>= -32767
and view his code he want a 10 characters long
may be a long number no a integer
can it be zeros by the left?
x = Len(AnyString) or x = Len(cst(val(AnyString)))
if IsNumeric(txtTextbox)=True
if round(val(txtTextbox),0)=t
If x > 10 Then MsgBox "You entered too many numbers": Exit Sub
If x < 10 Then MsgBox "You entered too few numbers": Exit Sub
else
MsgBox "You entered decimals values"
end if
end if
Hi
This is a very reliable way to detect if a string represents an Integer:
' TheString parameter is the string you want ot validate
Private Function IsInteger(TheString As String) As Boolean
Dim RefInt As Integer
Dim TInt
On Error GoTo Er1
RefInt = CInt(TheString)
TInt = Val(TheString)
On Error GoTo 0
If TInt = RefInt Then
IsInteger = True
Else
IsInteger = False
End If
Exit Function
Er1:
IsInteger = False
End Function
This is a very reliable way to detect if a string represents an Integer:
' TheString parameter is the string you want ot validate
Private Function IsInteger(TheString As String) As Boolean
Dim RefInt As Integer
Dim TInt
On Error GoTo Er1
RefInt = CInt(TheString)
TInt = Val(TheString)
On Error GoTo 0
If TInt = RefInt Then
IsInteger = True
Else
IsInteger = False
End If
Exit Function
Er1:
IsInteger = False
End Function
<< If x > 10 Then MsgBox "You entered too many numbers": Exit Sub
If x < 10 Then MsgBox "You entered too few numbers": Exit Sub>>
A length of 10 characters is not an integer, its a long type. Anyways, below you'll find how to make sure only numbers can be entered, and how to validate the length:
Private Sub Form_Load()
'set cause validation
Text1.CausesValidation = True
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
'make sure only numeric values are typed
If Not (IsNumeric(Chr(KeyAscii)) Or KeyAscii = 44 Or KeyAscii = 46 Or KeyAscii = 8) Then
KeyAscii = 0
End If
End Sub
Private Sub Text1_Validate(Cancel As Boolean)
'validate the length of the text in text1
If Len(Text1.Text) < 10 Then
MsgBox "You entered too few numbers"
ElseIf Len(Text1.Text) > 10 Then
MsgBox "You entered too many numbers"
End If
End Sub
S
If x < 10 Then MsgBox "You entered too few numbers": Exit Sub>>
A length of 10 characters is not an integer, its a long type. Anyways, below you'll find how to make sure only numbers can be entered, and how to validate the length:
Private Sub Form_Load()
'set cause validation
Text1.CausesValidation = True
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
'make sure only numeric values are typed
If Not (IsNumeric(Chr(KeyAscii)) Or KeyAscii = 44 Or KeyAscii = 46 Or KeyAscii = 8) Then
KeyAscii = 0
End If
End Sub
Private Sub Text1_Validate(Cancel As Boolean)
'validate the length of the text in text1
If Len(Text1.Text) < 10 Then
MsgBox "You entered too few numbers"
ElseIf Len(Text1.Text) > 10 Then
MsgBox "You entered too many numbers"
End If
End Sub
S
ASKER
I went with:
If IsNumeric(Text1.Text) = True Then
Exit Sub
Else
MsgBox "You must type only numbers": Text1.Text = ""
Thanks everyone!
If IsNumeric(Text1.Text) = True Then
Exit Sub
Else
MsgBox "You must type only numbers": Text1.Text = ""
Thanks everyone!
this will allow only integers to be entered
'=========================
Private Sub Form_Load()
Text1.MaxLength = 5
Text1.Text = ""
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> vbKeyBack Then
KeyAscii = 0
End If
End Sub
'=========================
;-)
Shiju