|
-
Apr 22nd, 2000, 04:09 AM
#1
Thread Starter
Member
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?
-
Apr 22nd, 2000, 04:43 AM
#2
Hyperactive Member
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
-
Apr 22nd, 2000, 08:24 AM
#3
Fanatic Member
Or do this:
NOTE: VB 5.0 Pro Example
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
[Edited by QWERTY on 04-22-2000 at 09:25 PM]
-
Apr 22nd, 2000, 09:05 AM
#4
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.
-
Apr 22nd, 2000, 09:20 AM
#5
Fanatic Member
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.
-
Apr 22nd, 2000, 10:29 AM
#6
Ah! Good catch. Sorry fo the mistake.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|