Results 1 to 6 of 6

Thread: Telling if a string is a #

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 1999
    Posts
    36
    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?
    -Boo

  2. #2
    Hyperactive Member
    Join Date
    Jun 1999
    Posts
    308
    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

  3. #3
    Fanatic Member
    Join Date
    Oct 1999
    Location
    MA, USA
    Posts
    523
    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]

  4. #4
    Guest
    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.

  5. #5
    Fanatic Member
    Join Date
    Oct 1999
    Location
    MA, USA
    Posts
    523
    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.

  6. #6
    Guest
    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
  •  



Click Here to Expand Forum to Full Width