Checking number of characters in a TextBox

P

Patrick C. Simonds

The code below check to ensure there is data in TextBox2 but I need to add
another If Statement to ensure that the number of characters in TextBox2
equals 3 characters.

If TextBox2.Text = "" Then 'Initials
MsgBox "Sorry but you must enter your initials"
MultiPage1.Value = 0
GoTo MissingData
End If
 
P

Patrick C. Simonds

Never mind

Figured it out.

If TextBox2.TextLength <> 3 Then
MsgBox "Sorry but you must enter a 3 character initials"
MultiPage1.Value = 0
GoTo MissingData
End If
 
R

Rick Rothstein

Try this...

If Not TextBox2.Text Like "???" Then
MsgBox "Sorry but you must enter your initials"
MultiPage1.Value = 0
GoTo MissingData
End If

You didn't show the rest of your code, so its hard to offer you an
alternative, but, personally, I would rather not see GoTo being used in code
like this except as part of an On Error GoTo statement.
 
J

JLGWhiz

This is a little nit-pickish but not everyone has three initials and some
people have more than three. Also some people add, jr. or sr. as part of
their initials. So you might have to use a message box to set the rules for
entering the initials.
 
P

Patrick C. Simonds

Thanks for your reply.

A 3 letter initial was a requirement imposed on me by the person requesting
the worksheet that I am developing. I did consider the fact that not
everyone has a middle name and amended my message to what you see below.


If TextBox2.TextLength <> 3 Then
MsgBox "Sorry but your initials must be 3 characters. Enter a ? for your
middle initial if you do not have one"
MultiPage1.Value = 0
GoTo MissingData
End If
 
P

Patrick C. Simonds

Thanks for your reply.

This code is part of a series of tests to ensure that all mandatory
TextBoxes are filled in. I would be happy to submit all of the code for you
but I was trying to keep the message down to a reasonable length.
 

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