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.

VB Code:
  1. Private Function ValidateKey(TextBoxName As TextBox, Keyascii As Integer) As Integer
  2. Dim strTagFilter As String
  3. Dim intPosition As Integer
  4.  
  5. If Keyascii = 8 Then
  6.     Exit Function
  7. Else
  8.     If TextBoxName.Tag & "" = "" Then
  9.         Exit Function
  10.     Else        intPosition = Len(TextBoxName.Text) + 1 'Sets up the Tag pointer
  11.         strTagFilter = Mid(TextBoxName.Tag, intPosition, 1) 'Gets the Validator Code
  12.        
  13.         If intPosition > Len(TextBoxName.Tag) Then 'Only allows the same number of characters
  14.                                           'that are contained in the Tag Property
  15.            
  16.             Keyascii = 0  'If so doesn't allow anymore characters
  17.         Else
  18.             TextBoxName.SelStart = intPosition 'Positions alphanumeric
  19.                                                 'character after previous
  20.                                                 'alphanumeric character
  21.             Select Case strTagFilter
  22.                 Case "A", "a" 'Only allows Letters
  23.                     If (Keyascii >= 65 And Keyascii <= 90) Or _
  24.                             (Keyascii >= 97 And Keyascii <= 122) Then
  25.                        
  26.                             Keyascii = Keyascii
  27.                         If strTagFilter = "A" Then 'Capitalize the letter if "A"
  28.                            If (Keyascii >= 97 And Keyascii <= 122) Then
  29.                                 Keyascii = Keyascii - 32 ' Capitalize the first charater
  30.                            End If
  31.                         End If
  32.                     Else
  33.                         Keyascii = 0 'invalid character
  34.                     End If
  35.                    
  36.                 Case "0"    'Only allows numbers
  37.                     If (Keyascii < 48 Or Keyascii > 57) Then
  38.                         Keyascii = 0 'invalid character
  39.                     End If
  40.                 Case Else
  41.                     If Keyascii <> Asc(strTagFilter) Then 'Only allows the same alphanumeric
  42.                                                           'Character (i.e. /-_=+&*()#$@!{}|\)
  43.                        Keyascii = 0 'invalid character
  44.                     End If
  45.             End Select
  46.         End If
  47.     End If
  48. End If
  49. End Function
... and you call it like this ...

VB Code:
  1. Private Sub Text1_KeyPress(Keyascii As Integer)
  2.     Call ValidateKey(Me.Text1, Keyascii)
  3. 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