INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Visual Basic (Microsoft) Versions 5/6 FAQ

Processing User Input

How to make sure only number are entered into a text box by swilliams
Posted: 25 Nov 00

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.

Back to Visual Basic (Microsoft) Versions 5/6 FAQ Index
Back to Visual Basic (Microsoft) Versions 5/6 Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close