© 1999, by Que Education and Training, Chapter 5, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach When are decisions used? n Give user greater control i Skip processing that doesn’t apply to user’s situation u Ex: A questionnaire that asks gender & pregnant i Select processing as requested by user u Ex: An investment program to find either the present value or future value n Data validation n Finding minimums and maximums
© 1999, by Que Education and Training, Chapter 5, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Simple Decisions If condition Then ‘ statement(s) End If If condition Then ‘ statement(s) for true Else ‘ statement(s) for false End If Condition ? TrueFalse Steps to process if condition is true Condition ? TrueFalse Steps to process if condition is true Steps to process if condition is false
© 1999, by Que Education and Training, Chapter 5, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Relational Operators Used to compare two values in a simple condition Comparing Strings: ASCII Collating Sequence (tear card) Use caution when comparing the text property of text boxes (as a variant, it may be considered a string or number, depending upon what it is being compared to)
© 1999, by Que Education and Training, Chapter 5, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Generalized Approach for Data Validation Function n Assume there will be a problem i Assign invalid return value to function n For each data value input by user, verify the correctness of the data prior to processing i If data is invalid, do the following: u Display message to user to specifying problem u Force user to re-enter data u Exit function n If all input checks were successful i Assign valid return value to function
© 1999, by Que Education and Training, Chapter 5, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Logical Operators Used to form compound (multiple) conditions
© 1999, by Que Education and Training, Chapter 5, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Logical Expressions for Validation n Check the invalid cases in the condition i Make sure that salary is between $500 and $2500: If Not (Salary >= 500 And Salary <= 2500) Then ‘ do error processing since outside range limits End If is equivalent to If Salary 2500 Then ‘ do error processing since outside range limits End If n When negating a complex condition, both the relational operators and logical operators must be reversed
© 1999, by Que Education and Training, Chapter 5, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Logical Expressions for Validation n Check the invalid cases in the condition n Make sure that age is between 21 and 75: If CInt(txtAge.Text) < 21 OR _ CInt(txtAge.Text) > 75 Then ‘ do error processing since outside range limits End If n Make sure single people have no spouse If txtStatus.Text = “single” AND _ txtSpouse.Text <> “” Then ‘ do error processing - spouse given for single person End If
© 1999, by Que Education and Training, Chapter 5, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Types of Validation Checks n Existence Check n Type Check n Range Check n Reasonableness Check n Code Check n Consistency Check
© 1999, by Que Education and Training, Chapter 5, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Existence Check n Used to verify that required data was input n AKA presence check n Compare the input value against the empty string. n Example: If last name is required input, use: If txtLast.Text = “” Then ‘ error processing End If
© 1999, by Que Education and Training, Chapter 5, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Type Check n Used to verify that the type of data input is correct n IsNumeric & IsDate functions i Verifies that input data consists only of valid numbers or legal dates n Example: If salary is a valid number: If Not IsNumeric(txtSalary.Text) Then ‘ error processing End If
© 1999, by Que Education and Training, Chapter 5, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Range Check n Used to verify that required data falls within valid limits n Compare the input value against the absolute end points n Example: If age must be positive: If CInt(txtAge.Text) <= 0 Then ‘ error processing End If
© 1999, by Que Education and Training, Chapter 5, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Reasonableness Check n Special case of range check n Used to verify that required data falls within expected limits n Example: If age is unlikely to be under 21: If CInt(txtAge.Text) < 21 Then ‘ error processing - warn user on possible problem End If
© 1999, by Que Education and Training, Chapter 5, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Code Check n Similar to range check n Look for specific matches (special codes) n Example: If the first two characters of employee ID must be 19: If Left(txtID.Text, 2) <> “19” Then ‘ error processing End If
© 1999, by Que Education and Training, Chapter 5, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Consistency Check n Used to verify that multiple input data does not establish invalid relationships n Examples: i Hospital bill processing program confirms that labor and delivery charges are for a female patient. i A single employee does not list a spouse. i A 12-year old cannot rent an adult video. n Multiple conditions checked in same condition
© 1999, by Que Education and Training, Chapter 5, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Displaying messages to user n MsgBox function i Displays modal dialog box to user i Waits for user to click button i Returns integer indicating which button was clicked
© 1999, by Que Education and Training, Chapter 5, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach MsgBox Function n Variable = MsgBox(MsgString, TypeOfWindow) i MsgString is a customized string that appears in the dialog window i TypeOfWindow identifies the icons & buttons displayed u vbOKOnly + vbInformation u vbYesNo + vbQuestion + vbDefaultButton2 n Displays a message & returns value of button user pressed. n Example: Response = MsgBox(“Do you want to continue?”, _ vbYesNo + vbQuestion + vbDefaultButton2) If Response = vbYes Then ‘ continue processing Else ‘ terminate processing EndIf
© 1999, by Que Education and Training, Chapter 5, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach MsgBox “Statement” n Displays a message, but does not keep track of which button user pressed. i Call MsgBox(MsgString, TypeOfWindow) n Example: If Not IsNumeric(txtAge.Text) Then Call MsgBox(“Age must be numeric. Please re-enter.”, _ vbOkOnly + vbInformation) EndIf
© 1999, by Que Education and Training, Chapter 5, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Force user to re-enter data n Change focus to erroneous textbox Call txtName.SetFocus n Highlight text in textbox txtName.SelStart = 0 txtName.SelLength = Len(txtName.Text)
© 1999, by Que Education and Training, Chapter 5, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach When to do data validation? n Text box’s Change event i Triggered on every minute change n Text box’s LostFocus event i Triggered immediately upon moving the focus out of a text box i Cascading events n Calculate button’s Click event i Call data validation function & bypass calculate processing if return value indicates invalid input
© 1999, by Que Education and Training, Chapter 5, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Data Validation Example 1 Private Sub cmdCalc_Click() If DataVal Then ‘ do normal processing End If End Sub Private Function DataVal() As Boolean DataVal = False ‘ default return value assumes a problem If Not IsNumeric(txtHours.Text) Then Call MsgBox(“Hours must be numeric-Please re-enter.”, _ vbOkOnly + vbInformation) Call txtHours.SetFocus Exit Function End If ‘ repeat similar decision structure for remaining input text boxes DataVal = True ‘ change return value since no problems discovered End Function
© 1999, by Que Education and Training, Chapter 5, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Data Validation Example 2 Private Sub cmdCalc_Click() If DataVal = False Then Exit Sub End If ‘ do normal processing End Sub Private Function DataVal() As Boolean DataVal = False ‘ default return value assumes a problem If Not IsNumeric(txtHours.Text) Then Call MsgBox(“Hours must be numeric-Please re-enter.”, _ vbOkOnly + vbInformation) Call txtHours.SetFocus Exit Function End If ‘ repeat similar decision structure for remaining input text boxes DataVal = True ‘ change return value since no problems discovered End Function
© 1999, by Que Education and Training, Chapter 5, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Minimums and Maximums n Generalized procedure is similar to the counting/accumulating process i Use higher-scope variable i Initialize before processing - two choices u In Form_Load assign maximum to smallest possible value and minimum to largest possible value u During processing, assign minimum and maximum to the first value –This requires special steps to track when the first value is processed i During processing, compare to determine if there is a new minimum or maximum
© 1999, by Que Education and Training, Chapter 5, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Program Testing Guidelines n Adding selective structures to your program requires extra steps for thorough program testing n Construct sample input data with provisions for each of the following: i Execute all statements at least once i Execute all branches of conditions i Enter different lengths of string input i Enter various types of numeric input i Test common error situations i Repeat the steps in a different order