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
Printable View
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
Add whatever numbers you need to accomplish your task to the Case statementHow you deal with the pasteing of what you would consider illegal characters is up to you, but you get the idea.VB Code:
Private Sub Text1_KeyPress(KeyAscii As Integer) Select Case KeyAscii Case 32 To 64, 91 To 96, 123 To 126 MsgBox ("Must be a letter! Please try again!") KeyAscii = 0 Exit Sub End Select End Sub 'now, this will not prevent someone from pasting non-alpha text 'into your textbox, so to address this issue, you will need to do 'something along the lines of Private Sub Text1_Change() If IsNumeric(Text1.Text) Then Msgbox "Only Letters Allowed" Text1.Text = "" End If End Sub
Hope this helps.
This will handle the keypreses:
VB Code:
Private Sub Form_Load() Debug.Print Asc("A") & " " & _ Asc("Z") & " " & _ Asc("a") & " " & _ Asc("z") & " " Text1.Text = "" End Sub ' I added #8 = BackSpace. Take it out if you don't want it ' Also, remove the beep if you don't want it. Private Sub Text1_KeyPress(KeyAscii As Integer) Select Case KeyAscii Case 8, 32, 39, 46, 47, 65 To 90, 97 To 122 Exit Sub Case Else Beep KeyAscii = 0 End Select End Sub
You want only one space in the textbox?