PDA

View Full Version : Adding zeros to numbers entered



av8tordude
07-30-2009, 11:34 AM
I have a textbox that the user will be entering only numbers. But I need them to enter at least 3 characters. So instead of the user just entering a number the code would enter a 0 in front.

i.e.

User types = 2 Code Enters = 002
User types = 23 Code Enters = 023
User types = 233 Code Enters = 233

Benzadeus
07-30-2009, 11:44 AM
Private Sub TextBox1_AfterUpdate()
TextBox1.Text = Format(TextBox1.Text, "000")
End Sub

av8tordude
07-30-2009, 11:55 AM
Hi Ben

It works like a charm!

How do I limit the number of characters entered. I tried to limit the number of characters in the properties, but when I enter 3, it prevents me from enter more numbers, but if I enter 4 in the properties, it doesn't prevent me from enter only 3 characters but allows me to enter 4 characters.

Benzadeus
07-30-2009, 12:24 PM
I didn't understand your question. Are you talking about property MaxLenght of the TextBox?

av8tordude
07-30-2009, 12:27 PM
I didn't understand your question. Are you talking about property MaxLenght of the TextBox?

Yes...When I enter 3, then the code only allows me to enter only 1 number, but if I enter 4 in the MaxLength, then the code doesn't not prevent me from entering only 3 characters.

Benzadeus
07-30-2009, 12:54 PM
I desagree. When I enter 3 in MaxLenght, I can enter 1, 2 or 3 characters in the TextBox.

mdmackillop
07-30-2009, 01:04 PM
I'm not totally clear if this is what you are after. It will pad out number to the MaxLength value


Private Sub TextBox1_AfterUpdate()
TextBox1.Text = Format(TextBox1.Text, Application.Rept("0", Me.TextBox1.MaxLength))
End Sub