Data Validation & 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.
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:
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.
|
||
|
|
||
|
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.
|
||
|
|