+ Reply to Thread
Results 1 to 4 of 4

Number Validation problem

Hybrid View

  1. #1
    Registered User
    Join Date
    04-12-2006
    Posts
    39

    Number Validation problem

    Hi, I have a Textbox that I want to force the user to type in a number. My validation code is not stoping the user from entering in alpha characters. As soon as I type in a alpha character I get an error. The debugger goes to the code right after the "Exit Sub" line. It appears that it is continuing to run the code when I would think it would be exiting the routine. Hoping someone can tell me what I'm doing wrong. Thanks in advance, Jody

    Private Sub TextBoxTargetCFMbuIn_Change()
    'Validation
    If (Not IsNumeric(TextBoxTargetCFMbuIn.Value)) And Len(TextBoxTargetCFMbuIn.Value) > 0 Then
    Me.TextBoxTargetCFMbuIn.Value = ""
    Me.TextBoxTargetCFMbuIn.SetFocus
    MsgBox "Numbers Only Please!"
    End If
    Exit Sub
    Me.LabelCFMbuHolder.Caption = Application.Text((Me.TextBoxTargetCFMbuFt.Value / Me.TextBoxTargetCFMbuIn.Value), "##.##")
    End Sub

  2. #2
    Forum Expert Carim's Avatar
    Join Date
    04-07-2006
    Posts
    4,070
    Hi,

    Something along these lines ...
    Private Sub textbox1_Exit(ByVal Cancel As 
    MSForms.ReturnBoolean) 
      If Not IsNumeric(textbox1.Text) Then 
        MsgBox "This textbox should contain a numeric value." 
        Cancel = True 
      End If 
    End Sub
    HTH
    Carim

  3. #3
    Forum Expert Carim's Avatar
    Join Date
    04-07-2006
    Posts
    4,070
    Hi,

    While testing just found out about commas or periods as decimals separator ...
    to overcome this issue too ...
    Private Sub TextBox1_KeyPress(KeyAscii As Integer) 
    Select Case KeyAscii 
    Case 8 To 10, 13, 27, 32 'Control characters 
        Case 44, 46 'comma, dot 
            If InStr(TextBox1.Text, ",") = 0 Then 
                KeyAscii = 44 
            Else 
                KeyAscii = 0 
            End If 
        Case 45 'minus 
           If TextBox1.SelStart = 0 And InStr(TextBox1.Text, "-") = 0 Then 
            Else 
                KeyAscii = 0 
            End If 
        Case 48 To 57 'numbers 
        Case Else 'Discard anything else 
            KeyAscii = 0 
    End Select 
    End Sub
    HTH
    Carim

  4. #4
    Registered User
    Join Date
    04-12-2006
    Posts
    39
    Thank You!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1