How to Limit a Text Box to Numbers Only in Visual Basic 6.0

Written by mia savchenko
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Limit a Text Box to Numbers Only in Visual Basic 6.0
Type numbers only into a text box. (Jupiterimages/Polka Dot/Getty Images)

Text boxes are primarily used to receive input from users, or to display text either preprogrammed or from a database source. In Visual Basic 6.0, the "TextBox" control is used to draw a text box into a Windows Form. The control's properties can be set depending on the user requirements. For example, you can set a text box to read-only or editable, with different background colour, or with scroll bars. It is also possible to limit what type of text input it can accept. Learn how to create a program in Visual Basic 6.0 that limits a text box input to numbers only.

Skill level:
Moderately Easy


  1. 1

    Load the Visual Basic 6.0 environment by clicking the program's shortcut icon from the desktop, or selecting "Microsoft Visual Basic 6.0" from the "Start" menu in your computer.

  2. 2

    Select "Standard EXE" from the "New Project" dialogue box, then click "Open" button.

  3. 3

    Add a text box into the form by clicking the "TextBox" control from the "Toolbox," then drawing the desired text box size. Make sure it's wide enough to hold input characters from the user.

  4. 4

    Clear the text caption by erasing the word "Text1" next to the "Text" property at the "Properties" window on the right side of the screen.

  5. 5

    Double-click the text box control to display the "Code" window. This is where you'll write the instructions or source codes that the program will execute at run-time. The source code you are going to write will validate user input and accepts only numeric characters, else it will display an error message.

  6. 6

    Insert the following code within the "Text1_Change" event:

    If Not IsNumeric(Text1.Text) Then

    MsgBox "Numeric input only!", vbCritical, "Invalid Input"
    Text1.Text = ""

    End If

  7. 7

    Press F5 or select "Run" then "Start" from the menu to switch to runtime-mode.

  8. 8

    Test the program to check that it's running as it should. Type alphanumeric characters or combination of letters and numbers into the text box. You should see an error message letting you know the input is invalid. Now, type only numbers into the text box. If no error message pops up, the program is running correctly.

  9. 9

    Save the program by selecting "Save Project" from the "File" menu. Provide a filename into the space provided, then click "Save."

Don't Miss

  • All types
  • Articles
  • Slideshows
  • Videos
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the site, you consent to the use of cookies. For more information, please see our Cookie policy.