Results 1 to 4 of 4

Thread: KeyAscii

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    81

    Question KeyAscii

    hi,

    I want a text box to accept only Alpha(A-Z or a - z) and / ' . (KeyAscii 39,46,47) and only one space. what is the simplest coding?

    Thanks,
    Viv

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: KeyAscii

    Add whatever numbers you need to accomplish your task to the Case statement
    VB Code:
    1. Private Sub Text1_KeyPress(KeyAscii As Integer)
    2. Select Case KeyAscii
    3.     Case 32 To 64, 91 To 96, 123 To 126
    4.         MsgBox ("Must be a letter! Please try again!")
    5.         KeyAscii = 0
    6.         Exit Sub
    7. End Select
    8. End Sub
    9.  
    10. 'now, this will not prevent someone from pasting non-alpha text
    11. 'into your textbox, so to address this issue, you will need to do
    12. 'something along the lines of
    13. Private Sub Text1_Change()
    14. If IsNumeric(Text1.Text) Then
    15.    Msgbox "Only Letters Allowed"
    16.    Text1.Text = ""
    17. End If
    18. End Sub
    How you deal with the pasteing of what you would consider illegal characters is up to you, but you get the idea.

    Hope this helps.

  3. #3
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: KeyAscii

    This will handle the keypreses:

    VB Code:
    1. Private Sub Form_Load()
    2. Debug.Print Asc("A") & " " & _
    3.             Asc("Z") & " " & _
    4.             Asc("a") & " " & _
    5.             Asc("z") & " "
    6. Text1.Text = ""
    7. End Sub
    8. ' I added #8 = BackSpace. Take it out if you don't want it
    9. ' Also, remove the beep if you don't want it.
    10.  
    11. Private Sub Text1_KeyPress(KeyAscii As Integer)
    12.   Select Case KeyAscii
    13.     Case 8, 32, 39, 46, 47, 65 To 90, 97 To 122
    14.       Exit Sub
    15.     Case Else
    16.       Beep
    17.       KeyAscii = 0
    18.  End Select
    19. End Sub

  4. #4
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: KeyAscii

    You want only one space in the textbox?

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