VB6 Tutorial 66: Validation

By | 2018-06-08

Validation is a very useful feature in VB6, which you implement in a variety of situations.

Many applications have login systems. So while coding for the login and registration forms, it is a must to include the validations for the controls on the forms.

What is validation?

You must have noticed in many applications that when you enter something incorrectly or when you leave the field blank, it warns you with a message. This is called validation.

More precisely, when you leave the text field and move to another control, you get a warning message and the input focus goes back to the previous field.

In short, validation tells the user to enter valid data.

I have already shown you an example of this type in the Registration program sample, but VB6 validation techniques were not used in that sample.

You may say that you can achieve it without using the validation code, but VB6 provides a better way, a more convenient way to achieve the same, which is obviously the better solution to the problem.

The Validation event and the CausesValidation property makes the solution, and sometimes the ValidateControls method is required. The default value of the CausesValidation property is True.

You have to write the code in the Validate event procedure of the control where you input the data, for example, on the TextBox control. After that, you move to another control, for example, to a CommandButton, a TextBox or any other. That means the first control where you entered the data lost the input focus, and the other control is about to receive the focus. If the CausesValidation property of the control that is about to receive the focus is True only then Visual Basic fires the Validate event of the first control that lost the focus.

QuickHint

Validation is also used to prevent your program from doing something it wasn’t meant to do. For example, SQL injection allows an attacker to use a program to extract, modify or delete information of a database.

By writing a validation procedure for user input, you can prevent your program from allowing a SQL injection attack through user inputs.

The IsNumeric function

What if the user enters a non-numeric i.e a string value in the txtPhone field? So now, you may want the users only to enter numeric values. The IsNumeric function can do it for you.

The function returns True if it is a numeric value.

If Not IsNumeric(txtPhone.txt) Then
   Cancel = True
End If 

The ValidateControls method

You generally use this method so that the user cannot close the form without validating first.

This method invokes the Validate event of the control that has the input focus. You generally use it in the Unload or QueryUnload event prcedure.

Private Sub Form_Unload(Cancel As Integer)
   On Error Resume Next
   ValidateControls
   If Err = 380 Then
      Cancel = True
   End If
End Sub

This method returns an Error 380 when the cancel parameter was set to True in the Validate event procedure of the control having the input focus

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.