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?
Printable View
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?
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
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:rolleyes:
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
YOU R THE MAN! Thanks a million, it certainly taste great!!:D
Regards,
Wilde