Check if textbox text is a number?-VBForums
Results 1 to 6 of 6

Thread: Check if textbox text is a number?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    292

    Check if textbox text is a number?

    Hi everyone?

    How can know if a textbox text it΄s a valid number?

    Thanks.

  2. #2
    Frenzied Member
    Join Date
    Nov 2001
    Location
    Mass USA
    Posts
    1,674

    Re: Check if textbox text is a number?

    isnumeric(textbox)

  3. #3
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: Check if textbox text is a number?

    A bit extended (regarding the "valid" term)
    VB Code:
    1. With Text1
    2.     If IsNumeric(.Text) Then
    3.         If InStr(1, .Text, ".") Or InStr(1, .Text, ",") Then
    4.             If Val(.Text) > 0 Then
    5.                 Debug.Print "is positive floating point number"
    6.             ElseIf Val(.Text) < 0 Then
    7.                 Debug.Print "is negative floating point number"
    8.             Else
    9.                 Debug.Print "is zero"
    10.             End If
    11.         Else
    12.             If Val(.Text) > 0 Then
    13.                 Debug.Print "is positive fixed point number"
    14.             ElseIf Val(.Text) < 0 Then
    15.                 Debug.Print "is negative fixed point number"
    16.             Else
    17.                 Debug.Print "is zero"
    18.             End If
    19.         End If
    20.     End If
    21. End With
    edit: oops... changed floating to fixed

  4. #4
    Frenzied Member zynder's Avatar
    Join Date
    Nov 2006
    Location
    localhost
    Posts
    1,434

    Re: Check if textbox text is a number?

    VB Code:
    1. Private Sub Command1_Click()
    2.     If IsNumeric(Text1) Then
    3.         MsgBox "Number"
    4.     Else
    5.         MsgBox "Not"
    6.     End If
    7. End Sub

  5. #5
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    32,844

    Re: Check if textbox text is a number?

    You can also download and compile the code for my NumberBox ocx from the link in my signature. It will prevent anything but numbers from being entered in the first place.

  6. #6
    Frenzied Member
    Join Date
    Nov 2001
    Location
    Mass USA
    Posts
    1,674

    Re: Check if textbox text is a number?

    if you would rather limit entry of anything besides numbers without using a ocx you could do the following in the textbox' keypress event:

    VB Code:
    1. Select Case KeyAscii
    2.      Case 13 'Enter has been pressed
    3.      
    4.      Case 48 To 57 '<- ascii values of 0 thru 9
    5.            'Do nothing
    6.     Case Else 'anything else besides a number or enter key turns to a 0 value.
    7.            KeyAscii = 0
    8. End Select

    I am gonna check out the ocx myself.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.