Documente Academic
Documente Profesional
Documente Cultură
/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.
2EMHFWLYHV
After completing this lab, you will be able to:
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:
([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.
… 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
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.
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.
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.
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.