Results 1 to 7 of 7

Thread: How do I check for letters vs numbers?

  1. #1

    Thread Starter
    Addicted Member Daniel_Christie's Avatar
    Join Date
    Jan 2000
    Location
    USA
    Posts
    245

    Post

    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

  2. #2
    Hyperactive Member
    Join Date
    Jul 2002
    Location
    Canada
    Posts
    455

    Post

    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.

  3. #3

    Thread Starter
    Addicted Member Daniel_Christie's Avatar
    Join Date
    Jan 2000
    Location
    USA
    Posts
    245

    Post

    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

  4. #4
    Frenzied Member Buzby's Avatar
    Join Date
    Jan 1999
    Location
    UK
    Posts
    1,670

    Post

    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]



  5. #5
    Hyperactive Member
    Join Date
    Jun 1999
    Location
    Calgary Alberta
    Posts
    359

    Post

    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.

  6. #6
    Frenzied Member
    Join Date
    Aug 1999
    Location
    Santa Clara, Ca , 95058
    Posts
    1,105

    Post

    Netsurfer meant to say the keyascii value of backspace is 8.

  7. #7

    Thread Starter
    Addicted Member Daniel_Christie's Avatar
    Join Date
    Jan 2000
    Location
    USA
    Posts
    245

    Post

    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
  •  



Click Here to Expand Forum to Full Width