Data Validation and Error Checking
_________________________
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 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
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.
|
![]() |
_________________________
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
_________________________
To check to see if a value has been entered in a field, check to see if the Text property is equal to "".
To obtain and check the value that was entered in a Text field, use the Val function.
To check to see if the value entered in a field is numeric, use the IsNumeric function.
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.
_________________________
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.
_________________________
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: To
highlight the invalid text, use the following sequence of commands: ---------------------------------------------- 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). '----------------------------------------------------------------------- |
_________________________