| Error handling is a bit different
in VBCE than in regular Visual Basic. Because the program flow in VBCE is basically
VBScript, there are many error handling techniques that you would normally use in Visual
Basic that you cannot use in VBCE. Examples of items that are not available in VBCE are On Error Goto MyErrorHandler, Resume, and Resume Next. Furthermore, runtime errors are not displayed the same in VBCE. If an
unhandled error occurs in VBCE, you simply get the following error message: 
The following code shows this error:
Private Sub
Command1_Click()
'Raise an error
Err.Raise 429
End Sub
Normally in Visual Basic you would get the following error:

There is one advantage to this, and that is that the program
does not terminate like a standard Visual Basic application would. The drawback is that
there is no indication of what the error was so that the user can report it, and it's
going to bring error handling to be nearly an art form when it comes to VBCE.
VBCE has only one method of handling errors, On Error Resume Next. To
ensure that the errors are reported, you should add at least the following code to each
procedure that you feel has any chance of failing:
Private Sub
Command1_Click()
On Error Resume Next
'Raise an error
Err.Raise 429
'Report the last error
If Err.Number
<> 0 Then
MsgBox "An error
has occurred in the Command1_Click Event:" & vbNewLine _
& Err.Number &
": " & Err.Description, vbCritical, _
"Program
Error"
End If
End Sub
This code sample above would produce the following message:

Although this does report if any error occurred in the
procedure, it has one drawback - it only reports the last error number, which may be
irrelevant because the first error that occurred may have caused the second error. The
first error goes unreported, but at least you know what routine is generating an error.
Things brings about the need for a reusable error reporting
and checking routine. The code below is called to check for an error. If an error has
occurred, it is reported and the user has the chance to end the application, or continue:
Private Sub
Command1_Click()
On Error Resume Next
'Raise an error
Err.Raise 429
'If an error occurs we
just report it
ReportError "Command1_Click"
'if an error occurs we do
not continue the sub
Err.Raise 438
If
ReportError("Command1_Click") Then Exit Sub
MsgBox "Sub code Complete"
End Sub
Public Function ReportError(ProcedureName)
'==================================
'Displays error message and returns
'Error number if there was an error
'==================================
Dim iMsgboxAnswer
ReportError = Err.Number
If Err.Number
<> 0 Then
iMsgboxAnswer =
MsgBox("An error has occurred in " _
& ProcedureName
& ":" & vbNewLine & vbNewLine _
& Err.Number &
": " & Err.Description & vbNewLine & vbNewLine & _
"Continue Program?
(Choosing No will end the program.)", _
vbCritical + vbYesNo,
"Program Error")
Err.Clear
Select
Case iMsgboxAnswer
Case vbYes: Exit Function
Case vbNo: App.end
End Select
End If
End Function
The code above generates the following dialog:

If the user chooses Yes, the program continues, if the user
chooses No the program ends. This function can be used two ways:
1) Just show the error and continue on yes, as shown in the
error 429 that we generated.
2) Show the error and then make a program flow decision based
on whether there was an error or not, as shown in the error 438 that we generated.
I'm sure it will take some getting used to, but with a little
practice you'll be handling VBCE program errors like a pro. Yes, it will take a bit more
coding to implement full error handling, but much of the code can be copied and pasted
throughout your program. If you wish to implement this by massive copy-and-paste, you may
wish to just call the ReportError routine with the name of your application instead of the
procedure name, like this:
If
ReportError("Hello World App") Then Exit Sub
If you have implemented a different error handling technique,
be sure to let me know.
webmaster@vbce.com |