Userform: text restrictions

G

Gert-Jan

Hi,

I'm working with a VBA userform; in the fields are only digits and letters
must be allowed. How do I make this restriction?

Thanks.
 
Z

Zack Barresse

Hi there Gert-Jan,

You could use a Change event like this ...

Private Sub TextBox1_Change()
Dim i As Long
For i = 1 To Len(Me.Textbox1.Text)
Select Case Asc(Mid(Me.Textbox1.Text, i, 1))
Case 48 To 57, 65 To 90, 97 To 122
Case Else
MsgBox "You have entered an illegal character!", vbInformation
Me.Textbox1.Value = Left(Me.Textbox1.Text,
Len(Me.Textbox1.Text) - 1)
Exit For
End Select
Next i
End Sub

HTH
 
Top