|
-
Apr 8th, 2005, 12:23 PM
#1
Thread Starter
Lively Member
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
-
Apr 8th, 2005, 12:27 PM
#2
Re: KeyAscii
Add whatever numbers you need to accomplish your task to the Case statement
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
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.
-
Apr 8th, 2005, 12:32 PM
#3
Re: KeyAscii
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
-
Apr 8th, 2005, 12:33 PM
#4
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|