100 likes | 208 Views
Decisions Nested on True Branch. False. True. Condition1 ?. Steps ABC. Steps to process if condition 1 is false. False. True. Condition2 ?. Steps to process if condition1 is true and condition2 is false. Steps to process if both condition1 and condition2 are true. Steps XYZ.
E N D
Decisions Nested on True Branch False True Condition1 ? Steps ABC Steps to process if condition 1 is false False True Condition2 ? Steps to process if condition1 is true and condition2 is false Steps to process if both condition1 and condition2 are true Steps XYZ Note the indentation If condition1 Then Statements for steps ABC If condition2 Then statement(s) for condition1 and condition2 true Else statement(s) for condition1 true and condition2 false End If Statements for steps XYZ Else statement(s) for condition1 false End If
Decisions Nested on False Branch False True Condition1 ? Steps ABC Steps to process False True if condition1 is Condition2 ? true Steps to process Steps to process if both condition1 and condition2 are false if condition1 is false and condition2 is true Steps XYZ Note the indentation If condition1 Then statement(s) for condition1 true Else Statements for steps ABC If condition2 Then statement(s) for condition1 false and condition2 true Else statement(s) for condition1 and condition2 false End If Statements for steps XYZ End If
Nested Decisions on False (ElseIf) False True Condition1 ? Steps to process False True if condition1 is Condition2 ? true Steps to process if both condition1 and condition2 are false Steps to process if condition1 is false and condition2 is true False branch contains a complete decision structure and no intervening steps before and after the decision If condition1 Then statement(s) for condition1 true ElseIf condition2 Then statement(s) for condition1 false and condition2 true Else statement(s) for both condition1 and condition2 false End If Note the indentation
Select Case: Special Case of Nested Decisions on False (ElseIf) Case expression Steps to process if cond1 True True cond1 False Steps to process if cond2 True True cond2 False Steps to process if condn True True condn Steps to process if condn False When every decision in nested if compares values against the same expression Select Case expression Case cond1 statement(s) for cond1 true Case cond2 statement(s) for cond1 false and cond2 true : Case condn statement(s) for cond1-condn-1 false and condn true Case Else statement(s) for al conditions false End Select Note the indentation
Limitations of Data Validation • It can become quite difficult to handle every type of problem during data validation that could arise. • VB provides the option of including error handlers to augment or as an alternative to data validation processing. • Supplying too large of a value to a conversion function will trigger an overflow error. • During division operations, a zero divisor will trigger a division by zero run time error. • Passing the wrong type of data to a function or procedure will trigger a type mismatch error.
VB’s Built-in Error Handling • What happens when errors occur in VB? • Number, source and description of VB’s Err object are updated • VB displays error message with above information • Program breaks or terminates
Writing Error Handlers • Instruct VB to bypass its error handling • On Error Goto PgmLabelName • Bypass error handler if no errors triggered • Add Exit Sub after last line of procedure body and right before End Sub • Add Error Handler code between Exit Sub & End Sub • Add Error Handler label • PgmLabelName: • Add Error Handler code
Error Handler Example 1 • Similar to VB’s built-in error handling • Important difference is that the program does not terminate execution Private Sub cmdCalc_Click() On Error GoTo HandleErrors ‘ override VB’s intrinsic error handling If DataVal Then ‘ do normal processing End If Exit Sub ‘ all done if it was the normal case HandleErrors: ‘ starting point of error handler Call MsgBox(Err.Number & “: “ & Err.Description, _ vbOkOnly+vbInformation) End Sub
Error Handler Example 2 Private Sub cmdCalc_Click() On Error GoTo HandleErrors If DataVal Then ‘ do normal processing End If Exit Sub HandleErrors: If Err.Number = 6 Then Call MsgBox(“Overflow occurred in formula”, vbOkOnly+vbInformation) ElseIf Err.Number = 11 Then Call MsgBox(“Division by 0 not allowed”, vbOkOnly+vbInformation) ElseIf Err.Number = 13 Then Call MsgBox(“Check data type compatibility”, vbOkOnly+vbInformation) Else Call MsgBox(Err.Number & “: “ & Err.Description, vbOkOnly+vbInformation) End If End Sub
Error Handler Example 3 Private Sub cmdCalc_Click() On Error GoTo HandleErrors If DataVal Then ‘ do normal processing End If Exit Sub HandleErrors: Select Case Err.Number Case 6 Then Call MsgBox(“Overflow occurred in formula”, vbOkOnly+vbInformation) Case 11 Call MsgBox(“Division by 0 not allowed”, vbOkOnly+vbInformation) Case 13 Call MsgBox(“Check data type compatibility”, vbOkOnly+vbInformation) Case Else Call MsgBox(Err.Number & “: “ & Err.Description, vbOkOnly+vbInformation) End Select End Sub