[RESOLVED] validation of a text box
hi i have a text box which does not have to exceed 10 characters code below:
Code:
If Len(text1.Text) > 10 Then
MsgBox "text box 1 length out of range"
End If
so when i type in some text and EXCEED the length of the text box (10) it gives me an message box. which is what i want. BUT after the message box has appeared i can click ok and happily type more characters in. so i was wondering what code i will need so there for more data cannot be entered and if it is then an error message appears?
thanks in advance
Re: validation of a text box
try like this
Code:
Private Sub Text1_Validate(Cancel As Boolean)
If Len(Text1.Text) > 10 Then
MsgBox "text box 1 length out of range"
Cancel = True
End If
End Sub
EDIT:
Also, if use set MaxLength property to 10 for the text box, this could be done, Only thing you wont get the message box.
:wave:
Re: validation of a text box
it does not work how i want it to work.
at the moment i have a text box which has a length of 10 characters, and if i exceed the number of characters, then an error message appears but after clicking ok i can add more characters i have tried to use max length but i want the text box NOT to be able to accept more characters and not to give a system beep, but an error message.
thanks for the quick reply.
Re: validation of a text box
try this:
Code:
Private Sub Text1_LostFocus()
If Len(Text1) > 10 Then
MsgBox "larger than 10, please enter again", vbExclamation
Text1.SetFocus
End If
End Sub
if you leave this text box, an error msg will be popped up. hope this helps
Re: validation of a text box
@ tjroamer
If you have noticed, the Validate event does the same. ;)
Only thing is its the proper place for it and it supports the Cancel parameter which cancel the focus change.
@adam786
Why it doesnt work like you want?
:wave: