Hi,
How can I block a user from typing non-numeric characters (a,b,c,d...) in a textbox adn display a message??
Herick
Printable View
Hi,
How can I block a user from typing non-numeric characters (a,b,c,d...) in a textbox adn display a message??
Herick
yes...
I use this code:
private Sub Text1_KeyPress(KeyAscii as Integer)
If InStr("ABCDEFGHIJKLMNOPQRSTUVWXYZ", UCase$(Chr$(KeyAscii))) <> 0 then KeyAscii = 0: Beep
End Sub
You can add other characters such as "?\'/;:.>,<" to the "ABCD...." string if you want.
cool!!! But now, how do I clear the contents of the textbox after displaying a message???? I try "txtsearch.text = "" but that adds a space in the textbox...how do I clear it?
There are many ways of doing it but here is the way I like to do it and you don't need to provide a message.
private Sub Text1_KeyPress(KeyAscii as Integer)
Dim strValid as string
strValid = "1234567890"
' if it's not a control code
If KeyAscii > 26 then _
If InStr(strValid, Chr(KeyAscii)) = 0 then _
KeyAscii = 0
End Sub
that should work... just make sure that you didn't write txtsearch.text = " ". That would put a space in the text box! :-)
If I type a "d" or any letter, I make a popup message to appear...after clicking ok , the letter is still there. here is the code:
If KeyAscii = 8 Then Exit Sub
If Not IsNumeric(Chr(KeyAscii)) Then
MsgBox "Please Enter only Alpha-Numeric Characters."
txtSearch.Text = ""
End If
I need to totally clear whatever they typed before.
Herick
that is because the "d" is placed in txtsearch after your code executes... you must reset KeyAscii to zero in place of txtSearch.text = "" like so:
If KeyAscii = 8 Then Exit Sub
If Not IsNumeric(Chr(KeyAscii)) Then
MsgBox "Please Enter only Alpha-Numeric Characters."
KeyAscii = 0
End If
actually I did not think of it but you can just add the KeyAscii = 0 statement... I was forgetting that your textbox could have more than the type chararter in it and you wanted to total clear it