Data Validation & Error Checking

 

Data Validation

Data validation can be accomplished through the use of nested Ifs.  If the program needs to verify the validity of the input data before calculations are attempted, the structure shown in the pseudocode below can be used.

If (dataItem1 fails test1) OrElse (dataItem1 fails test2) Then
    Notify user
    Require that dataItem1 be re-entered
ElseIf  (dataItem2 fails test1) OrElse (dataItem2 fails test2)  Then
    Notify user
    Require that dataItem2 be re-entered    
ElseIf  (dataItem3 fails test1) OrElse (dataItem3 fails test2) Then
    Notify user
    Require that dataItem3 be re-entered    
.
.
.
ElseIf  (dataItemn fails test1) OrElse (dataItemn fails test2) is invalid Then
    Notify user
    Require that dataItemn be re-entered    
Else
    Read input data
    Perform calculations as specified
End If

Notice that if any invalid data is detected, the calculations are not performed.  Instead, one of the other Else branches is taken, bypassing the Else branch that performs the calculations.  

If the calculations are not placed inside of an Else branch, but are instead placed outside of the If/Then/Else structure, then the program will detect an error but still attempt to perform the calculations.  The pseudocode shows this incorrect approach.

If dataItem1 is invalid Then
    Notify user
    Require that dataItem1 be re-entered
ElseIf dataItem2 is invalid Then
    Notify user
    Require that dataItem2 be re-entered    
ElseIf dataItem3 is invalid Then
    Notify user
    Require that dataItem3 be re-entered    
.
.
.
ElseIf dataItemn is invalid Then
    Notify user
    Require that dataItemn be re-entered    
End If

Read input data
Perform calculations as specified

_________________________

When performing data validation, there are several conditions that you should consider to see if they interfere with the normal processing.  Input data may be subject to any of the following conditions:

  • no value is entered

  • an incorrect numeric type is entered

  • text is entered instead of numeric value

  • negative value is entered when positive value is expected

  • 0 value is entered 

  • incorrect range of values may be entered

The order in which you test conditions matters. For example, if user input fails the numeric test then there is no point in checking to see if it is greater than 0.

_________________________

To check to see if a value has been entered in a field, check to see if the Text property is equal to "", i.e., txtLoan.Text = ""

To obtain and check the value that was entered in a Text field, use the Val function, i.e., to check to see if a value is positive write Val(txtLoan.Text) < 0

To check to see if the value entered in a field is numeric, use the IsNumeric function, ie., Not (IsNumeric(txtLoan.Text))

To check to see if a value is not an integer, there are various tests, such as (num - (num \ 1)  <> 0).

If an invalid value is entered into a text box, display a message box on the screen asking them to provide the required input and return the focus to the unfilled text box using the Focus method.   Code to highlight the invalid entry is included below.

 


Error Checking

One of the basic problems to look for when writing error checking routines is conditions that can adversely affect mathematical calculations. Among the most obvious conditions are a divide by zero and a logarithm of 0 (Log(0)). Neither of these conditions are valid.  The first step is to look closely at the calculations to see if any of them offer the potential for a either condition. 

In the mortgage program the formula for calculating factor requires that the divisor, decIntPerPayment, not equal 0.  If it is, then the formula for calculating factor must be skipped, or there must be an alternative way to calculate factor.  In this program, if the decIntPerPayment equals 0, factor can be set to totalPayments.

_________________________

Highlighting invalid entries

In order to allow the user to correct invalid input more easily, return the focus to the control in question, and then highlight the invalid entry in the text box so that they don't have to erase the existing entry before entering the correct value.

Use the Focus method to move the focus to a particular control:
txtInput.Focus

To highlight the invalid text, use the following sequence of commands:
txtInput.SelectionStart = 0                                  ' Set cursor to beginning of box
txtInput.SelectionLength = Len(txtInput.Text)     ' Highlight text

----------------------------------------------

If you want, you can copy the following sub into your program, and each time that you need to require the user to re-enter data, you can call this sub by passing the text box name as a parameter, such as Call textHighlighter(txtTempF).

'-----------------------------------------------------------------------
' This sub accepts a text box as a parameter, sets the focus to that box,
' places the cursor at the beginning of the box, and highlights the text.
'-----------------------------------------------------------------------
Private Sub textHighlighter(txtVar As TextBox)
     txtVar.Focus
     txtVar.SelectionStart = 0                            ' Set cursor to beginning of box
     txtVar.SelectionLength = Len(txtVar.Text) ' Highlight text
End Sub