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
LVL 1
rinkydinkAsked:
Who is Participating?
 
SethiCommented:
You can use IsNumeric fucntion to see whether a number has been entered. Then you can use the maximum value of an integer to know whether its an integer. Example:

if IsNumeric(txtTextbox)=True then
   if txtTextbox<=32767 And txtTextbox>=-32767 then
     Msgbox "Its an integer"
   else
     Msgbox "Its not an integer"
   end if
end if
0
 
Shiju SasidharanAssoc Project ManagerCommented:
Hi try this

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
0
 
dbeneitCommented:
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)=txtTextbox 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
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
iHadiCommented:
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
0
 
ShauliCommented:
<< 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
0
 
rinkydinkAuthor Commented:
I went with:

If IsNumeric(Text1.Text) = True Then
Exit Sub
Else
MsgBox "You must type only numbers": Text1.Text = ""



Thanks everyone!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.