Allowing Only Letters In TextBox

Knight Of Nih

New member
Local time
Tomorrow, 04:18
Joined
Nov 16, 2004
Messages
7
I have a form like in the picture I provided
I want people to be able to enter letters in there only
How can I do that?
And where do I put the code

Thanks
Knight Of Nih
 

Attachments

  • Untitled-2.jpg
    Untitled-2.jpg
    23.7 KB · Views: 242
Use the KeyPress event for the text box to only allow alpha characters. Check the help files for ASCII keys if you need to add more.
Code:
Private Sub TextBox1_KeyPress(KeyAscii As Integer)
    
    Select Case KeyAscii
        Case 97 To 122, 65 To 90
            KeyAscii = KeyAscii
        Case Else
            MsgBox "Only alpha characters are allowed.", vbInformation, "Invalid Key"
            KeyAscii = 0 'key nothing
    End Select
    
End Sub
 
I tried your method but unfortunately I had no success
I ended up finding this though

Code:
Private Sub txtName_keypress(ByVal keyascii As MSForms.ReturnInteger)

If keyascii >= Asc("A") And keyascii <= Asc("z") Then
keyascii = Asc(Chr(keyascii))
Else
keyascii = 0
End If

End Sub

I do thank you for your help though.
 
Trouble with those routines, is that it doesn't allow you to use backspace if you make a mistake. or some people have hyphen's or spaces in their name which doesn't allow for.

.
 

Users who are viewing this thread

Back
Top Bottom