I need to know if a string that a user gives me is an integer. If it has any letters or other characters in it, I want the user to get a msgbox telling him/her to type in a number.
How do I do this?
Printable View
I need to know if a string that a user gives me is an integer. If it has any letters or other characters in it, I want the user to get a msgbox telling him/her to type in a number.
How do I do this?
This code disallows the user to type any letters:
Code:Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii < Asc("0") Or KeyAscii > Asc("9") Then
KeyAscii = 0 ' Cancel the character.
Beep ' Sound error signal.
End If
End Sub
Or do this:
NOTE: VB 5.0 Pro Example
[Edited by QWERTY on 04-22-2000 at 09:25 PM]Code:Private Sub Command1_Click()
Dim stString As String
stString = InputBox("Enter String", "IsNumeric Example")
If Not IsNumeric(stString) Then '//IsNumeric checks if string contains only numbers
MsgBox "Incorrect Input!!!"
End If
End Sub
It doesn't look like the other solutions check to see if it is an Integer or not. If I wanted to know if a number is an integer I would use this:
X = 5
Z = 5.5
Round(X) = X
That would return true
Round(Z) = Z
That one would return false
Hope this helps. :)
It will give you an error if string contains something else besides numbers though. So you have to use IsNumeric function to prevent that error.
Ah! Good catch. Sorry fo the mistake.