Textbox

R

ranswrt

How do I make sure that whatever is entered in a textbox on a userform is a
letter of the alphabet or a number only?
Thanks
 
J

Joel

try something like this

Function TestString(TestStr As String)

TestString = True
If Not IsNumeric(TestStr) Then
For i = 1 To Len(TestStr)
TestChar = UCase(Mid(TestStr, i, 1))

If Asc(TestChar) < Asc("A") Or _
Asc(TestChar) > Asc("Z") Then

TestString = False
Exit For
End If
Next i
End If

End Function
 
R

RyanH

I just made some modifications to Joel post. Since you only want Letters and
Numbers in the textbox it will probably be very useful to use a
AfterUpdate_Event. This will test the Text in the Textbox once the Textbox
looses its focus. I added a message box as well.

Private Sub TextBox1_AfterUpdate()

Dim TestStr As String
Dim I As Integer
Dim TestChar As String

TestStr = TextBox1.Text

For I = 1 To Len(TestStr)
TestChar = UCase(Mid(TestStr, I, 1))

If Not IsNumeric(TestChar) Then
If Asc(TestChar) < Asc("A") Or _
Asc(TestChar) > Asc("Z") Then
MsgBox "You need to enter Letters & Numbers only!", vbExclamation
Exit For
End If
End If
Next I

End Sub

Hope this helps!
 
R

ranswrt

Thanks I'll give that a try

RyanH said:
I just made some modifications to Joel post. Since you only want Letters and
Numbers in the textbox it will probably be very useful to use a
AfterUpdate_Event. This will test the Text in the Textbox once the Textbox
looses its focus. I added a message box as well.

Private Sub TextBox1_AfterUpdate()

Dim TestStr As String
Dim I As Integer
Dim TestChar As String

TestStr = TextBox1.Text

For I = 1 To Len(TestStr)
TestChar = UCase(Mid(TestStr, I, 1))

If Not IsNumeric(TestChar) Then
If Asc(TestChar) < Asc("A") Or _
Asc(TestChar) > Asc("Z") Then
MsgBox "You need to enter Letters & Numbers only!", vbExclamation
Exit For
End If
End If
Next I

End Sub

Hope this helps!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top