Click to See Complete Forum and Search --> : How do I check for letters vs numbers?
Daniel_Christie
Jan 27th, 2000, 01:12 PM
Let's say I have a textbox and I want the user to only place in a Name. I can easily write code to validate that there is a character entered, but I don't know how to force the user to only use letters. In other words. How do I check to make sure that the characters entered into a textbox is letters instead of being either empty or numbers?
I appreciate any time and effort,
Daniel Christie
michelle
Jan 27th, 2000, 01:45 PM
Hello Daniel Christie,
double klick on your textbox and choose in the upper right kolom KeyPress.
fill in:
Private Sub Text1_KeyPress(KeyAscii As Integer)
If Not (Chr(KeyAscii) >= "a" And Chr(KeyAscii) <= "z") Then KeyAscii = 0
End Sub
nice regards,
michelle.
Daniel_Christie
Jan 27th, 2000, 02:21 PM
Wow michelle,
That certainly was very effective, I like it.
There is just one thing. How would I Use that code and still beable to allow the "Backspace" as a valid Keypress?
I am terribly sorry to bother you further,
Daniel Christie
Buzby
Jan 27th, 2000, 04:37 PM
Another method would be;
private sub Textbox1_KeyPress(KeyAscii as Integer)
If Isnumeric(chr(Keyascii)) then Keyascii=0
End sub
This prevents numbers being used (but still allows other characters).
Another method, if you want to prevent all sorts of characters being entered;
Private Sub TextBox1_Keypress(Keyascii as integer)
If Instr("01234567890-!',;",chr(keyascii))>0 then keyascii=0
end sub
This will prevent any characters between the double quotes being entered - note if you want to include double quotes (") in this set of characters you should use;
If Instr("0123456790"+chr(34),....
as chr(34)= "
Hope this helps.
By the way Michelle's solution would prevent capital letters being entered.
------------------
Mark "Buzby" Beeton
VB Developer
BuzbyB@HotMail.Com
netSurfer
Jan 27th, 2000, 08:21 PM
OR
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 8 Then
elseif (KeyAscii) < 48 And KeyAscii > 57 and KeyAscii > 31 then
else
keyascii = 0
End Sub
The keyascii = backspace
ascii 48 to 57 are numbers 0 to 9
ascii 31 and below aren't supported.
you can use this to capture any of the characters you want to allow them to use. Another way:
If (KeyAscii) > 47 And KeyAscii < 58 then
Keysacii = 0
End Sub
That would change all numbers to ascii 0. If you wanted to estop them from using other chars you can just use their ascii numbers.
JHausmann
Jan 28th, 2000, 12:41 AM
Netsurfer meant to say the keyascii value of backspace is 8.
Daniel_Christie
Jan 28th, 2000, 07:49 AM
Thank you all very much,
After reading all of your feedback, I ended up desingning this code:
Private Sub txtOwners_KeyPress(KeyAscii As Integer)
If Not (KeyAscii = 32) And Not (KeyAscii = 8) And Not (Chr(KeyAscii) >= "A" And Chr(KeyAscii) <= "z") Then
KeyAscii = 0
End If
End Sub
Worked Perfectly!
Thank you for all the great feedback Guys.
Daniel Christie
PS.(for give me if this cut-n-paste looks unformatted, but you know what I mean)
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.