|
-
Jul 15th, 2002, 11:33 PM
#1
Thread Starter
New Member
[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.
-
Jul 16th, 2002, 12:40 AM
#2
Hyperactive Member
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
-
Jul 16th, 2002, 03:02 AM
#3
Thread Starter
New Member
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
-
Jul 16th, 2002, 03:21 AM
#4
Hyperactive Member
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
-
Jul 16th, 2002, 03:49 AM
#5
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|