Mark Gambo
Dec 2nd, 2006, 06:00 PM
A while back I wrote the following function becase I had a need to restrict the way a user is to have entered data into a text box. This function allows the programmer the ability to control what type of alphanumeric character is entered into a text box at a particular postion in the text box.
Private Function ValidateKey(TextBoxName As TextBox, Keyascii As Integer) As Integer
Dim strTagFilter As String
Dim intPosition As Integer
If Keyascii = 8 Then
Exit Function
Else
If TextBoxName.Tag & "" = "" Then
Exit Function
Else intPosition = Len(TextBoxName.Text) + 1 'Sets up the Tag pointer
strTagFilter = Mid(TextBoxName.Tag, intPosition, 1) 'Gets the Validator Code
If intPosition > Len(TextBoxName.Tag) Then 'Only allows the same number of characters
'that are contained in the Tag Property
Keyascii = 0 'If so doesn't allow anymore characters
Else
TextBoxName.SelStart = intPosition 'Positions alphanumeric
'character after previous
'alphanumeric character
Select Case strTagFilter
Case "A", "a" 'Only allows Letters
If (Keyascii >= 65 And Keyascii <= 90) Or _
(Keyascii >= 97 And Keyascii <= 122) Then
Keyascii = Keyascii
If strTagFilter = "A" Then 'Capitalize the letter if "A"
If (Keyascii >= 97 And Keyascii <= 122) Then
Keyascii = Keyascii - 32 ' Capitalize the first charater
End If
End If
Else
Keyascii = 0 'invalid character
End If
Case "0" 'Only allows numbers
If (Keyascii < 48 Or Keyascii > 57) Then
Keyascii = 0 'invalid character
End If
Case Else
If Keyascii <> Asc(strTagFilter) Then 'Only allows the same alphanumeric
'Character (i.e. /-_=+&*()#$@!{}|\)
Keyascii = 0 'invalid character
End If
End Select
End If
End If
End If
End Function
... and you call it like this ...
Private Sub Text1_KeyPress(Keyascii As Integer)
Call ValidateKey(Me.Text1, Keyascii)
End Sub
So to allow the following number to be entered into a text box US35-39234B then enter th following into that textboxes Tag Property: AA00-00000A
Private Function ValidateKey(TextBoxName As TextBox, Keyascii As Integer) As Integer
Dim strTagFilter As String
Dim intPosition As Integer
If Keyascii = 8 Then
Exit Function
Else
If TextBoxName.Tag & "" = "" Then
Exit Function
Else intPosition = Len(TextBoxName.Text) + 1 'Sets up the Tag pointer
strTagFilter = Mid(TextBoxName.Tag, intPosition, 1) 'Gets the Validator Code
If intPosition > Len(TextBoxName.Tag) Then 'Only allows the same number of characters
'that are contained in the Tag Property
Keyascii = 0 'If so doesn't allow anymore characters
Else
TextBoxName.SelStart = intPosition 'Positions alphanumeric
'character after previous
'alphanumeric character
Select Case strTagFilter
Case "A", "a" 'Only allows Letters
If (Keyascii >= 65 And Keyascii <= 90) Or _
(Keyascii >= 97 And Keyascii <= 122) Then
Keyascii = Keyascii
If strTagFilter = "A" Then 'Capitalize the letter if "A"
If (Keyascii >= 97 And Keyascii <= 122) Then
Keyascii = Keyascii - 32 ' Capitalize the first charater
End If
End If
Else
Keyascii = 0 'invalid character
End If
Case "0" 'Only allows numbers
If (Keyascii < 48 Or Keyascii > 57) Then
Keyascii = 0 'invalid character
End If
Case Else
If Keyascii <> Asc(strTagFilter) Then 'Only allows the same alphanumeric
'Character (i.e. /-_=+&*()#$@!{}|\)
Keyascii = 0 'invalid character
End If
End Select
End If
End If
End If
End Function
... and you call it like this ...
Private Sub Text1_KeyPress(Keyascii As Integer)
Call ValidateKey(Me.Text1, Keyascii)
End Sub
So to allow the following number to be entered into a text box US35-39234B then enter th following into that textboxes Tag Property: AA00-00000A