1. Start a new project. Add a text box and a command button.
2. Set the properties of the form and each control:
Form1:
BorderStyle - 1-Fixed Single
Caption - Error Generator
Name frmError
Command1:
Caption - Generate Error
Default - True
Name - cmdGenError
Text1:
Name - txtError
Text - [Blank]
The form should look something like this:
3. Attach this code to the cmdGenError_Click event.
Private Sub cmdGenError_Click()
On Error GoTo HandleErrors
Err.Raise Val(txtError.Text)
Err.Clear
Exit Sub
HandleErrors:
Select Case MsgBox(Error(Err.Number), vbCritical + vbAbortRetryIgnore, "Error
Number" + Str(Err.Number))
Case vbAbort
Resume ExitLine
Case vbRetry
Resume
Case vbIgnore
Resume Next
End Select
ExitLine:
Exit Sub
End Sub
In this code, we simply generate an error using the number input in the text box.
The generic error handler then displays a message box which you can respond to
in one of three ways.
4. Save your application. Try it out using some of these typical error numbers
(or use numbers found with on-line help). Notice how program control changes depending
on which button is clicked.
Error Number Error Description
6 Overflow
9
Subscript out of range
11 Division
by zero
13 Type
mismatch
16 Expression
too complex
20 Resume
without error
52 Bad
file name or number
53 File
not found
55 File
already open
61 Disk
full
70 Permission
denied
92 For
loop not initialized