Validating number of characters in textbox

B

Blondegirl

Hi. I want to validate the data entry on a textbox so that a message box
appears if the user inputs a reference less than 7 digits long. (part of
a form which adds a new record to the sheet) I've set the textbox to
maxlength of 7 but can't get the code right. Please could someone
assist. Here is what I have so far which doesn't work:

If not TextBox1.MaxLength Then
MsgBox "The entry has to be 7 digits long"
TextBox1.SetFocus
Exit Sub
End If

Thank you.
 
M

mudraker

Try

If not len(TextBox1) = 7 Then
MsgBox "The entry has to be 7 digits long"
TextBox1.SetFocus
Exit Sub
End If
 
A

aidan.heritage

Do it on the EXIT event of textbox1 as follows

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If Len(TextBox1) <> 7 Then
MsgBox "Text must be 7 characters long"
Cancel = True
End If
End Sub
 

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