Results 1 to 9 of 9

Thread: Simple validation to ensure a textbox value is a number?

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2005
    Posts
    38

    Simple validation to ensure a textbox value is a number?

    Hey,

    Real sorry if this is stupidly obvious, but what is the validation I can use in a command box to ensure some text boxes I have contain a number, so if not it displays an error message.

    Would really appreciate your help.
    Thanks.

  2. #2
    eltiT resU motsuC Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Simple validation to ensure a textbox value is a number?

    command box?? u mean command button?

    VB Code:
    1. If Not IsNumeric(Text1) Then
    2.      MsgBox "Error! Text1 is not a number"
    3.      Text1.SetFocus
    4. End If
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  3. #3

    Thread Starter
    Member
    Join Date
    Apr 2005
    Posts
    38

    Re: Simple validation to ensure a textbox value is a number?

    Quote Originally Posted by Static
    command box?? u mean command button?

    VB Code:
    1. If Not IsNumeric(Text1) Then
    2.      MsgBox "Error! Text1 is not a number"
    3.      Text1.SetFocus
    4. End If
    Hey, thanks for the reply.

    Is it possible that I can use this for more than one text field in one code, e.g.:

    If Not IsNumeric(Text1, Text2, Text3) Then
    etc.

    I tried that, but it returned an error.

  4. #4

  5. #5
    eltiT resU motsuC Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Simple validation to ensure a textbox value is a number?

    use marty's control... or

    u can code it like this..

    If Not IsNumeric(Text1) or Not IsNumeric(Text2) or Not IsNumeric(Text3) Then
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  6. #6
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    367

    Re: Simple validation to ensure a textbox value is a number?

    Quote Originally Posted by sugardaddy
    Hey,

    Real sorry if this is stupidly obvious, but what is the validation I can use in a command box to ensure some text boxes I have contain a number, so if not it displays an error message.

    Would really appreciate your help.
    Thanks.
    Here's some coding I did today that will only allow a user to key in numbers and nothing else. The second set of coding will display an error message if the user tries to copy/paste letters and other non-numeric data into the box. The second set of coding is the same as what the others said. BUT, with the first set of coding it just flat out prevents the user from using the keyboard to type in non-numeric data into the textbox and they won't get annoyed by error messages constantly popping up unless they try to copy/paste data into the textbox.

    VB Code:
    1. Private Sub txtEmployee_KeyPress(KeyAscii As Integer)
    2. 'Only allows the user to use numbers and backspace on the keyboard for
    3. 'the textbox txtEmployee
    4. If (KeyAscii >= 48 And KeyAscii) <= 57 Or KeyAscii = 8 Then
    5.      Exit Sub
    6. Else
    7.      KeyAscii = 0
    8. End If
    9. End Sub
    10.  
    11. Private Sub txtEmployee_LostFocus()
    12. 'Checks for non-numeric characters if user tries to copy/pastes in data
    13.     If Not IsNumeric(txtEmployee(Index)) And txtEmployee(Index) <> "" Then
    14.         MsgBox ("Please use numbers only!")
    15.         txtEmployee(Index).SetFocus
    16.     End If
    17. End If

  7. #7
    eltiT resU motsuC Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Simple validation to ensure a textbox value is a number?

    dont use lost focus.. use validate

    then cancel = true when errors...
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  8. #8
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    367

    Re: Simple validation to ensure a textbox value is a number?

    Quote Originally Posted by Static
    dont use lost focus.. use validate

    then cancel = true when errors...
    Yea, you can use Validate instead. I just thought it only activated when you tried to save/delete/etc data. Didn't know it activated also when the control lost focus.

  9. #9
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Simple validation to ensure a textbox value is a number?

    If the textbox loses focus to a control that has CausesValidation = True (the default) then it will fire, otherwise it won't.

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