exact number of characters in textbox

G

Guest

The program I am developing requires employees to enter information about
customer numbers. These customer numbers are always 11 digits long. I know
how to limit the textbox to 11 characters but how do I check to see if all 11
characters were entered?
 
T

Tom Ogilvy

in the appropriate event for the textbox, use the len function

if len(me.Textbox1.Text) <> 11 then

There are two kinds of text boxes and textboxes can be located on
worksheets, userforms, dialogsheets, and a more specific answer would
require more specific knowledge although you will probably get some guesses.
 
B

Bob Phillips

If Len(Textbox1.Text) < 11 Then
MsgBox "Too few digits"
End If

--

HTH

RP
(remove nothere from the email address if mailing direct)
 
G

Guest

Thank you Tom.

I did forget to include that this is on a userform. Will what you put below
still work?

Thank again
 
E

Executor

Hi,

In this case you can use the Exit event for the textbox.

If you use this code it should work:

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Dim intPos As Integer
If Len(TextBox1.Text) <> 11 Then
MsgBox "PLEASE ENTER 11 DIGTIS", vbExclamation, "Retry"
Cancel = True
Else
For intPos = 1 To 11
If Not IsNumeric(Mid(TextBox1.Text, intPos, 1)) Then
MsgBox "Please use numbers only", vbExclamation,
"Retry"
Cancel = True
End If
Next
End If
End Sub

your user smust fill the textbox with 11 digits.
As a bonus I have added a check on numbers opnly.


HTH,

Wouter
 
E

Executor

Hi,

Some extra info:

You might need to change

IsNumeric(Mid(TextBox1.Text, intPos, 1))

into

IsNumeric(Mid(TextBox1.Text; intPos; 1))

where de , in changed into ;

this depents on regional setting.

Wouter
 

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