Validation in Visual Basic 6


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.



Examples:

Validate example1
  































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.



Example:

Take two TextBox controls, a submit button; set their CausesValidation property to True. Take a Cancel button, set its CausesValidation property to False. 

Set the TabIndex value of the txtName control to 0.



Validate demo VB6



  












Download this sample.




Private Sub txtName_Validate(Cancel As Boolean)
    If txtName.Text = "" Then
        MsgBox "Please enter your name!", vbExclamation, ""
        Cancel = True
    End If
End Sub
When the Cancel parameter is True, the input focus goes back to the txtName control.



Private Sub txtPhone_Validate(Cancel As Boolean)
    If Len(txtPhone.Text) < 10 Then
        MsgBox "Enter the phone number in 10 digits!", vbExclamation, ""
        Cancel = True
    End If
End Sub
In this case, the input focus moves back to the txtPhone control when the Cancel parameter is set to True.




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 = TrueEnd 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.

Download this sample here.