Presentation is loading. Please wait.

Presentation is loading. Please wait.

© 1999, by Que Education and Training, Chapter 6, pages 289-312 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach.

Similar presentations


Presentation on theme: "© 1999, by Que Education and Training, Chapter 6, pages 289-312 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach."— Presentation transcript:

1 © 1999, by Que Education and Training, Chapter 6, pages 289-312 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Decisions Nested on True Branch 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 Condition1 ? FalseTrue Steps to process if condition 1 is false True Condition2 ? False Steps to process if condition1 is true and condition2 is false Steps to process if both condition1 and condition2 are true Steps ABC Steps XYZ

2 © 1999, by Que Education and Training, Chapter 6, pages 289-312 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Decisions Nested on False Branch 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 Condition1 ? TrueFalse Steps to process if condition1 is true Condition2 ? TrueFalse Steps to process if condition1 is false and condition2 is true Steps to process if both condition1 and condition2 are false Steps ABC Steps XYZ

3 © 1999, by Que Education and Training, Chapter 6, pages 289-312 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Nested Decisions on False (ElseIf) 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 Steps to process if condition1 is false and condition2 is true Condition1 ? TrueFalse Steps to process if condition1 is true Condition2 ? TrueFalse Steps to process if both condition1 and condition2 are false False branch contains a complete decision structure and no intervening steps before and after the decision

4 © 1999, by Que Education and Training, Chapter 6, pages 289-312 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Select Case: Special Case of Nested Decisions on False (ElseIf) 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 When every decision in nested if compares values against the same expression cond1 True False Steps to process if cond1 True Case expression cond2 True False Steps to process if cond2 True condn True Steps to process if condn True Steps to process if condn False

5 © 1999, by Que Education and Training, Chapter 6, pages 289-312 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Limitations of Data Validation n It can become quite difficult to handle every type of problem during data validation that could arise. n VB provides the option of including error handlers to augment or as an alternative to data validation processing. i Supplying too large of a value to a conversion function will trigger an overflow error. i During division operations, a zero divisor will trigger a division by zero run time error. i Passing the wrong type of data to a function or procedure will trigger a type mismatch error.

6 © 1999, by Que Education and Training, Chapter 6, pages 289-312 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach VB’s Built-in Error Handling n What happens when errors occur in VB? i Number, source and description of VB’s Err object are updated i VB displays error message with above information i Program breaks or terminates

7 © 1999, by Que Education and Training, Chapter 6, pages 289-312 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Writing Error Handlers n Instruct VB to bypass its error handling i On Error Goto PgmLabelName n Bypass error handler if no errors triggered i Add Exit Sub after last line of procedure body and right before End Sub n Add Error Handler code between Exit Sub & End Sub i Add Error Handler label u PgmLabelName: i Add Error Handler code

8 © 1999, by Que Education and Training, Chapter 6, pages 289-312 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Error Handler Example 1 n Similar to VB’s built-in error handling n 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

9 © 1999, by Que Education and Training, Chapter 6, pages 289-312 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach 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

10 © 1999, by Que Education and Training, Chapter 6, pages 289-312 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach 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


Download ppt "© 1999, by Que Education and Training, Chapter 6, pages 289-312 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach."

Similar presentations


Ads by Google