Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to make sure only number are entered into a text box

Processing User Input

How to make sure only number are entered into a text box

by  swilliams  Posted    (Edited  )
Use the _Validate event of the text box to check that the entry is numeric.

To use the sample code below:
1. Create a project with just one form.
2. Add to the form a variety of controls, including a text box named Text1.
3. Copy one or the other routines below and paste into the form module.
4. Press F5.
5. When the code is running, click in the text box and then try to move focus elsewhere.

You will see that you can enter anything in the text box, but the text box will not lose focus until the entry is correct (numeric).

Private Sub Text1_Validate(Cancel As Boolean)
If Not IsNumeric(Text1.Text) Then Cancel = True
End Sub


This will not allow entry of nothing in the text box, so to allow that as well, use the code:

Private Sub Text1_Validate(Cancel As Boolean)
If Not IsNumeric(Text1.Text) And Len(Text1.Text) <> 0 Then Cancel = True
End Sub


This is a simple way to restrict entry in terms of the code that has to be written.
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top