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.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.