|
-
Jan 27th, 2000, 02:12 PM
#1
Thread Starter
Addicted Member
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
-
Jan 27th, 2000, 02:45 PM
#2
Hyperactive Member
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.
-
Jan 27th, 2000, 03:21 PM
#3
Thread Starter
Addicted Member
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
-
Jan 27th, 2000, 05:37 PM
#4
Frenzied Member
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
[email protected]
-
Jan 27th, 2000, 09:21 PM
#5
Hyperactive Member
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.
-
Jan 28th, 2000, 01:41 AM
#6
Frenzied Member
Netsurfer meant to say the keyascii value of backspace is 8.
-
Jan 28th, 2000, 08:49 AM
#7
Thread Starter
Addicted Member
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)
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
|