Validating text box entries in VBA


Posted by Alan H on April 10, 2001 1:23 PM

I have user form with a text box for entering letters only (either lower or upper case)into cells. How can I validate the text box entries so that the user can only enter letters and not numbers or other characters? I know how to do it in VBA for validating text box entries so that only numbers between a certain range but not for letters. Any help would be appreciated.

Posted by Dave Hawley on April 10, 2001 9:33 PM


Hi Alan

How's this

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If IsNumeric(TextBox1) Then
MsgBox "Entry must be text", vbCritical
Cancel = True
End If
End Sub


Dave

OzGrid Business Applications

Posted by Ivan Moala on April 11, 2001 6:27 AM

Dave

2 alternatives to Daves which will aloow you to
intercept keys before they are entered;

Private Sub TextBox2_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)

Select Case KeyAscii
Case 65 To 90, 87 To 122

Case Else
KeyAscii = 0
End Select
End Sub

Const Alpha As String = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"


Private Sub TextBox3_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If InStr((Alpha), Chr(KeyAscii)) = 0 Then KeyAscii = 0
End Sub


Ivan



Posted by Alan H on April 11, 2001 12:07 PM

Thanks guys.