restrict users to input atleast five characters

S

sam

how do we restrict user to input minimun characters in a text box on userform.

for eg. I want to make zip code field such that users need to input atlest 5
characters, and not anything less then that.

thanks in advance
 
E

Eduardo

Hi ,

Go to visual basic, view object, position in the textbox you want to enter
the limit, then choose Categorized, under Behavior look into MaxLength and
enter 5 there
 
M

Mike H

Hi,

There's the maxlength that you asked about earlier but no minlength so you
have to check yourself. Right click the textbox, view code and try this code

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If Len(TextBox1.Text) < 5 Then
With Me.TextBox1
.Text = ""
.SetFocus
End With
MsgBox "A properly formatted zipcode please"
End If

End Sub

Mike
 
R

Rick Rothstein

You could use this Exit event code for the TextBox (assumed to be named
TextBox1 for this example)...

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If Len(TextBox1.Text) > 0 And Len(TextBox1) < 5 Then
Cancel = True
MsgBox "Your entry must be at least 5 characters long."
End If
End Sub

As written, the code allows the user to exit the TextBox if there is no
entry in it (that is what the >0 test provides for).
 

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