|
-
Jul 17th, 2000, 07:10 PM
#1
Thread Starter
Hyperactive Member
Hi,
Below is the code that limits user's input to letters & digits only (in a TextBox). It works great, however I have 3 text boxes on my form & would like to check them all WITHOUT retyping the same rutine 3 times.
Can you guys let me know how to write all-purpose Sub/Function that will do the same thing for any text box (not just 'Text1'). Go Reusability!!!
As always Thanks for your help.
---------------------------------------------------------
Private Sub Text1_KeyPress(KeyAscii As Integer)
Dim strChar As String
' Accept the key if its a "control character" ...
If KeyAscii < 32 Then
Exit Sub
End If
strChar = Chr$(KeyAscii)
' Accept the key if it's numeric ...
If strChar >= "0" And strChar <= "9" Then
Exit Sub
End If
strChar = UCase$(strChar)
' Accept the key if it's alpha ...
If strChar >= "A" And strChar <= "Z" Then
Exit Sub
End If
' Otherwise, reject the key ...
KeyAscii = 0
End Sub
---------------------------------------------------------
-
Jul 17th, 2000, 07:26 PM
#2
Hyperactive Member
This should work. If you want to put the CheckText in a Module, just change the declaration to public.
Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
Dim lret As Long
lret = CheckText(KeyAscii)
KeyAscii = lret
End Sub
Private Function CheckText(ByVal KeyAscii As Integer) As Long
Dim strChar As String
' Accept the key if its a "control character" ...
If KeyAscii < 32 Then
CheckText = KeyAscii
Exit Function
End If
strChar = Chr$(KeyAscii)
' Accept the key if it's numeric ...
If strChar >= "0" And strChar <= "9" Then
CheckText = KeyAscii
Exit Function
End If
strChar = UCase$(strChar)
' Accept the key if it's alpha ...
If strChar >= "A" And strChar <= "Z" Then
CheckKey = KeyAscii
Exit Function
End If
' Otherwise, reject the key ...
KeyAscii = 0
CheckText = KeyAscii
End Function
Hope this helps
(VB 6 SP3)
-
Jul 17th, 2000, 07:34 PM
#3
Hyperactive Member
I think you have missed something...
Try following these steps :
1. Go to Notepad
2. Type in the following ---> abc:$%
3. Highlight this text and copy it into the clipboard (Ctrl-C)
4. Switch to your application
5. Place the cursor in your text box that only accepts letters and digits
6. Paste into this textbox (Ctrl-V)
7. Say "Oops"
-
Jul 17th, 2000, 07:43 PM
#4
Thread Starter
Hyperactive Member
Ok,
This seems to work:
Private Sub txtBox_KeyPress(KeyAscii As Integer)
LettersAndNumbersOnly KeyAscii
End Sub
The LettersAndNumbersOnly is a all-purpose Subrutine that I just wrote with all the checking (see above).
As far as Gen-X remark, Oops, you're right Gen-X, do you have any idea how to solve that?
-
Jul 18th, 2000, 12:01 AM
#5
Hyperactive Member
try this....
Hi,
Try this out in the Change event of the textbox ......to solve what Gen-X brought up:
Code:
Private Sub Text1_Change()
Dim i As Integer
Dim MyStr As Integer
Dim temp, temp1 As String
temp = Text1.Text
For i = 1 To Len(temp)
MyStr = Asc(Mid(temp, i, 1))
If ((MyStr >= 65 And MyStr <= 90) Or (MyStr >= 97 And MyStr <= 122) Or (MyStr >= 48 And MyStr <= 57)) Then
temp1 = temp1 & Chr(MyStr)
End If
Next
Text1 = temp1
SendKeys "{END}"
End Sub
[Edited by rammy on 07-18-2000 at 05:19 AM]
-
Jul 18th, 2000, 12:28 AM
#6
Hyperactive Member
The change event is executed every time you press a key (So it will run after you have executed the keypress event).
So I would probably do the following :
Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
KeyAscii = ValidateKey(KeyAscii)
End Sub
Private Sub Text1_Change()
Dim i as Integer
Dim sTemp as string
For i = 1 to Len(Text1.Text)
If ValidateKey(Asc(Mid(Text1.Text,i,1))) <> 0 Then
sTemp = sTemp & Mid(Text1.Text,i,1)
End If
Next i
Text1.Text = sTemp
End Sub
Private Function ValidateKey(ByVal KeyAscii as Integer) as Integer
ValidateKey = KeyAscii
Select Case KeyAscii
Case 1 to 32 ' Control Characters
Case Asc("0") to Asc("9") ' Numbers
Case Asc("a") to Asc("z") ' Lower case
Case Asc("A") to Asc("Z") ' Upper case
Case Asc(".") ' If you want decimal as well
Case Else
ValidateKey = 0
End Select
End Function
-
Jul 18th, 2000, 01:18 AM
#7
Hyperactive Member
was concentrating on the copy/paste bit.....so forgot abt the change event being fired every time a key is pressed....
-
Jul 18th, 2000, 02:11 AM
#8
Hyperactive Member
As for redoing the code for all the boxes, why not use an array? (Text1(0), Text1(1), Text1(2)) It works really well.
bob
-
Jul 18th, 2000, 03:15 AM
#9
Lively Member
...or, instead of the array, or retyping, why not this:
Code:
Private Sub Text1_Change()
DoValidate Text1
End Sub
Private Sub DoValidate(txbText As TextBox)
Dim i As Integer
Dim sTemp As String
For i = 1 To Len(txbText.Text)
If ValidateKey(Asc(Mid(txbText.Text, i, 1))) <> 0 Then
sTemp = sTemp & Mid(txbText.Text, i, 1)
End If
Next i
txbText.Text = sTemp
End Sub
Of course, add the ValidateKey function...
//Anders
Reality is what you make up when you can't handle your fantasies.
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
|