Sunteți pe pagina 1din 6

277

/DE#43=#(UURU#7UDSSLQJ
In this lab, you will write code that uses the Shell statement to attempt to run another application. If the
Shell statement fails, it can produce a run-time error. You can use a number of error-handling techniques
to deal with this error.

To see a demonstration of the lab solution, click this icon.


(CD-ROM plays the demonstration, "Lab 10 Solution.")

Estimated time to complete this lab: 30 minutes

2EMHFWLYHV
After completing this lab, you will be able to:

• Handle errors inline.


• Create an error handler.
• Determine how to exit the error handler.
• Create a centralized error-handler.

To complete the exercises in this lab, you must have the required software. For detailed information about
the labs and setup for the labs, see Labs in this course.

The solution for this lab is located in the folder <install folder>\Labs\Lab10\Solution.

3UHUHTXLVLWHV
Before working on this lab, you should be familiar with the following:

• Adding and working with procedures.

([HUFLVHV
The following exercises provide practice working with the concepts covered in Chapter 10.

([HUFLVH#4=#8VLQJ#,QOLQH#(UURU#+DQGOLQJ
In this exercise, you will check for errors in your code using inline error handling.

([HUFLVH#5=#&UHDWLQJ#DQ#(UURU0+DQGOLQJ#5RXWLQH
In this exercise, you will trap errors in the event procedure using an error-handling routine.

([HUFLVH#6=#&UHDWLQJ#DQ#(UURU0+DQGOLQJ#)XQFWLRQ
In this exercise, you will create a function for running applications. This function will return True if it was
able to shell the application and False if it was not.
Lab 10: Error Trapping

277 ([HUFLVH#4=#8VLQJ#,QOLQH#(UURU#+DQGOLQJ
In this exercise, you will check for errors in your code using inline error handling.

X Open the loan project


• Open either the loan project that you have been working on, or the Loan project in the <install
folder>\Labs\Lab10 folder.

X Create event procedure


… 1. Add a command button to the main form. Set the Name property to cmdOtherApp, and the Caption
property to &Other Applications, as shown in the following illustration.

… 2. In the Click event procedure for the Other Applications button, add the code that prompts the user for
an application name and uses the Shell statement to execute the application, as shown in the following
code:
Dim strAppName As String
strAppName = InputBox("To run the Calculator type 'calc'. " & _
"To run Notepad type 'notepad'.")
Shell strAppName, vbNormalFocus

… 3. Run the application and test the Other Application button by typing calc (the Windows calculator
application).
… 4. Test the Other Application button again by typing an invalid application name, such as XYZ. This will
produce a run-time error. Click the End button.

Notes

Page 64
Lab 10: Error Trapping

X Add inline error-handling code


… 1. At the beginning of the cmdOtherApp_Click event procedure, add the On Error Resume Next
statement to prevent Visual Basic from raising an exception when an error occurs.
… 2. After the Shell statement, check the Number property of the Err object to see if an error occurred. For
example, if an error occurred, display a message box with the error number.
Private Sub cmdOtherApp_Click()
'Keep running if error occurs
On Error Resume Next
Dim strAppName As String
strAppName = InputBox("To run the Calculator type 'calc'. " & _
"To run Notepad type 'notepad'.")

Shell strAppName, vbNormalFocus


'Check the Number property to see if there was an error
If Err.Number <> 0 Then 'If an error occurred
MsgBox "Unable to find " & strAppName & _
vbCrLf & "Run-time error. Number " & Err.Number
End If
End Sub

Note The vbCrLf string constant is the same as Chr(13) & Chr(10); this forces a carriage return–
linefeed combination which places any text that follows on the next line.

… 3. Test the application.

Notes

Page 65
Lab 10: Error Trapping

277 ([HUFLVH#5=#&UHDWLQJ#DQ#(UURU0+DQGOLQJ#5RXWLQH
In this exercise, you will trap errors in an event procedure by using an error-handling routine.

X Create an event procedure


… 1. Run the application, and in the main form, delete the value in the Interest Rate combo box.
… 2. Click on the Monthly Payments command button. This should produce a run time error.
… 3. What is the run-time error number?
To see additional information to help you complete the exercise, click this icon.
(CD-ROM displays a hint.)
… 4. Click the Debug button. Notice the line of code where the error occurred.
… 5. Click the End button.

X Create an error handler


… 1. Set an error trap by using the On Error statement in the MonthlyPayment function.
… 2. Create the error-handling routine.
In the error-handling routine, add code to test for different errors that could be generated. To see an
example of how your code should look, click this icon.
(CD-ROM displays a hint.)
… 3. Run and test the application. Does the error handler trap the error generated in the previous example?
To see additional information to help you complete the exercise, click this icon.
(CD-ROM displays a hint.)

Notes

Page 66
Lab 10: Error Trapping

277 ([HUFLVH#6=#&UHDWLQJ#DQ#(UURU0+DQGOLQJ#)XQFWLRQ
In this exercise, you will create a function for running applications. This function will return True if it was
able to shell the application and False if it was not.

X Create a function procedure


… 1. In the Click event procedure for the Other Application button, add code that calls a function named
RunApp, and test the return value of this function. If the result is false, display a message box telling
the user that the application was not found.
Your code should look similar to the following example code:
Private Sub cmdOtherApp_Click()
Dim strAppName As String
strAppName = InputBox("To run Calculator type 'Calc'. " & _
"To run Notepad type 'NotePad'.")
If RunApp(strAppName) = False Then
MsgBox "Unable to find " & strAppName & vbCrLf & _
"Error Number " & Err.Number
End If

End Sub

… 2. In the main form, create a form-level function procedure named RunApp. Make sure you do the
following:
a. Handle run-time errors, using either inline error handling or an error-handling routine.
b. Return True if the Shell statement was successful, and return False if an error occurred.
To see an example of how your code should look, click this icon.
(CD-ROM displays a hint.)
… 3. Test the procedures.

Notes

Page 67
This page intentionally left blank.

S-ar putea să vă placă și