Results 1 to 5 of 5

Thread: [RESOLVED] Only numbers, alphbets and a hypen?

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2002
    Posts
    11

    Thumbs up [RESOLVED] Only numbers, alphbets and a hypen?

    can anyone tell me how to allow only numbers alphabets(upper & lower case) and hypens(-) in a text box?

    i tried using Asc(), Chr$(), isnumber, and all sorts of combinations but they r all wrong.

    any ideas?
    Last edited by Wildec2; Jul 17th, 2002 at 03:11 AM.

  2. #2
    Hyperactive Member JMvVliet's Avatar
    Join Date
    May 2001
    Location
    Papendrecht, Netherlands
    Posts
    310
    In the KeyUp (or KeyDown) event of the textbox, test if the inputted character (KeyCode) is between 0 and 9, or if it is a hyphen or a backspace, else, set keycode = 0 to avoid this character from being displayed in the textbox:

    private sub KeyDown(.......)

    if (chr(keycode) >= 0 and chr(keycode) < 9) _
    or chr(keycode) = "-" or keycode = 8 then '8 = backspace
    'do nothing
    else
    keycode = 0
    endif
    end sub

    Hope this will help,

    grtz,

    JMvV

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2002
    Posts
    11
    Thanks for the codes. However nothing happen. this is what i've done so far. what do you suppoe went wrong?

    Private Sub cboNRIC_KeyDown(KeyCode As Integer, Shift As Integer)
    On Error GoTo ErrorHandler

    If (Chr(KeyCode) >= 0 And Chr(KeyCode) < 9) _
    Or Chr(KeyCode) = "-" Or KeyCode = 8 Then '8 = backspace
    'do nothing
    Else
    KeyCode = 0

    End If

    Exit Sub
    ErrorHandler:
    Call ErrorMessage
    End Sub



    Wilde

  4. #4
    Hyperactive Member JMvVliet's Avatar
    Join Date
    May 2001
    Location
    Papendrecht, Netherlands
    Posts
    310
    I'm very sorry, but I've done it wrong. The following code must be pasted in the KeyPress event:

    Private Sub cboNRIC_KeyPress(KeyAscii As Integer)
    On Error GoTo ErrorHandler

    If (KeyAscii >= Asc("0") And KeyAscii <= Asc("9")) Or KeyAscii = Asc("-") Or KeyAscii = vbKeyBack Then '8 = backspace
    'do nothing
    Else
    KeyAscii = 0
    End If

    Exit Sub
    ErrorHandler:
    MsgBox Err.Number & vbcrl & Err.Description
    End Sub

    That's all. I've tested it, and it works deliciously.

    grtz

    jmvv

  5. #5

    Thread Starter
    New Member
    Join Date
    Jul 2002
    Posts
    11

    Thumbs up

    YOU R THE MAN! Thanks a million, it certainly taste great!!

    Regards,

    Wilde

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