Form Err.Raise error not trapped by entry procedure error handler

J

jchauvin

I am having trouble getting my error handling to trap custom errors
generated by the Err.Raise command. My application is composed of user
forms and class modules without any standard modules. The Error
Trapping option is set to "Break on Unhandled Errors". My error
handling system is very simple and is designed to terminate the
application if any custom errors are detected. The error handler is
placed in the entry procedure with all custom errors within the various
forms or classes to be handled by this routine. Here is the code for
the entry procedure which is located in ThisWorkbook and called from
the Workbook_Open() Sub procedure:

Private Sub Workbook_Open()
Call OpenMainForm
End Sub

Public Sub OpenMainForm()
'
Dim frmMainForm As FMainForm

Set frmMainForm = New FMainForm

On Error GoTo ErrorTrap

Call frmMainForm.InitializeForm

frmMainForm.Show vbModal

Unload frmMainForm

Set frmMainForm = Nothing

Exit Sub

ErrorTrap:
MsgBox Err.Description, vbCritical, Err.Source

End Sub

Within the various user forms and class modules are validation checks
which raise errors if a problem is detected:

Err.Raise 1, "Sub HandleRunEditInfo", "Selection is not a valid
range."
Exit Sub

The expected behavior is for VBA to work up the Call Stack until it
encounters the error handler in the entry procedure located in
ThisWorkbook. Instead, VBA is displaying an unhandled error message.
There are no other error handlers (i.e. On Error statements) anywhere
within the forms or classes except for the entry procedure.

The unhandled error is being raise within the main form as a result of
a change in one of the controls but not being trapped by the error
handler contained in ThisWorkbook. I did try and move the above
routine to a standard module. This did not resolve the problem.

Any suggestion would be appreciated.

Thanks,

John C.
 
R

Robert ap Rhys

I am having trouble getting my error handling to trap custom errors
generated by the Err.Raise command. My application is composed of user
forms and class modules without any standard modules.

<....>

Hi John,

The problem is this:

Write some code to show a form. Write some event code in the form and
include a breakpoint. Now execute it to the breakpoint. Now check the call
stack. See that line [<non-basic code>]? That's VB's underlying plumbing
that shows forms by 'magic'. You can't raise COM errors through that. This
means that any errors raised in your forms can't be automatically trapped in
your calling code.

One solution to this is to have a central error-handling module that
includes all of your logic for dealing with both expected and unexpected
errors. Alternatively, you could raise a custom event in your form. Just
remember to declare the instances of your form(s) WithEvents.

HTH

Robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top