Visual Basic 6.0 Guide
Validating Data

What Is Validation?

Validation means checking data after or during input to the computer. There are many occasions where we might want to control the kinds of input that the user will make to the computer. We can do this in a number of ways in our programs.

  • Not executing code if the input data is invalid
  • Repeatedly asking for input until the right type of data is entered
  • Not allowing the user to enter invalid data

Not Doing Anything If The Data Is Invalid

1. Create a new project. Add a textbox called txtEnterNumber and a command button called cmdSquare.

Our program will should allow the user to enter a whole number from 0 to 20. If the user enters a valid number, the program squares the number and messageboxes the result. If they don't we tell them they have made a mistake and then do nothing.

2. Click on View, Code on the menu. Now write the function that will perform the calculation.

Private Function squareNumber(intNumber As Integer) As Integer
squareNumber = intNumber * intNumber
End Function

3. Next we need a function to check that our box contains a whole number.

Private Function isWholeNumber(strNumber As String) As Boolean
If IsNumeric(strNumber) = False Then
isWholeNumber = False
ElseIf Int(strNumber) <> Val(strNumber) Then
isWholeNumber = False
ElseIf Int(strNumber) < 0 Or Int(strNumber) > 20 Then
isWholeNumber = False
isWholeNumber = True
End If
End Function

4. Now we need to create the event procedure that is executed when the command button is clicked. Double click on the button and enter the following code.

Private Sub cmdSquare_Click()
Dim strText As String, intNumber As Integer, intResult As Integer
strText = txtEnterNumber.Text
If isWholeNumber(strText) = False Then
MsgBox "You Must Enter A Whole Number From 1 to 20"
intNumber = Int(strText)
intResult = intNumber * intNumber
MsgBox intResult
End If
End Sub

Test the program.

Keep Asking Until You Get The Right Answer

We can reuse the isWholeNumber function to make our program truly irritating.

Double click the form to bring up the load event procedure. Add this code.

Private Sub Form_Load()
Dim strEntered As String
strEntered = InputBox("Enter a whole number from 0 to 20")
Loop Until isWholeNumber(strEntered) = True
End Sub

Run your program and try to enter invalid data.

Adapt this code so that the user has to enter the word of your choice before they can use this program.

This technique can also be used to allow the user to enter an open-ended list of data. A particular character or phrase entered triggers the end of the process. At this point, the program does something else.

Prevent The User From Entering Invalid Data

When a user types in a textbox, a Keypress event is triggered. We can program the event procedure for this event so that the text box is cleared if the user enters anything other than a digit from 0 to 9.

In the code window, choose txtEnterNumber from the dropdown box in the top left of the window. Choose Keypress from the dropdown menu on the right.

You should see this code.

Private Sub txtEnterNumber_KeyPress(KeyAscii As Integer)

End Sub

The variable, KeyAscii stores the Ascii code of the key that was pressed. The computer uses a separate number to represent each character. The Ascii codes for the charcters from 0 to 9 range from 48 to 57. If the value of KeyAscii is not in that range, the user has entered something other than a number.

Our code should then read

Private Sub txtEnterNumber_KeyPress(KeyAscii As Integer)
If KeyAscii < 48 Or KeyAscii > 57 Then
MsgBox "Enter Numbers"
txtEnterNumber.Text = ""
End If
End Sub

Try it out and glory in the power that validation can bring.

Oops! What happens if the user wants to correct a mistake and innocently presses the backspace key? They lose their number unfairly. The backspace key has the Ascii code number 8. Can you alter the program to allow backspacing?

The arrow and delete keys do not trigger keypress events.