Err.Raise + Call Stack + UserForm

T

Trevor

I am experiencing an issue where a run-time exception does not make
its way back to the top-most subroutine. The run-time exception is
passed to the default Visual Basic error handler rather than being
handled by the exception handler of `Foo3' (see below).

Why does the error raised in the UserForm object not propagate back to
the subroutine in the public module (i.e. the MsgBox prompt is never
executed)?

The following subroutine is intended to create a call stack:

Public Sub RaiseError( _
ByRef r_lSubID As Long, _
ByRef r_lErrNum As Long, _
ByRef r_sErrDescrp As String, _
Optional ByRef r_sSubDescrp As String = Empty _
)
Call Err.Raise( _
Number:=vbObjectError + r_lSubID, _
Description:=r_sSubDescrp & vbCrLf & Format( _
expression:=IIf(r_lErrNum < 0, _
r_lErrNum - vbObjectError, r_lErrNum), _
Format:=g_sERR_NUM_FMT) & vbTab & r_sErrDescrp)
GoTo Finally
Finally:
Exit Sub
End Sub

The following subroutine (found in a class module named `clsFoo')
generates an error:

Public Sub Class_Initialize()
Dim foo As Variant
On Error GoTo ErrorHandler
'...
let foo = 1/0
'...
Goto Finally
ErrorHandler:
Dim lErrNum As Long
Dim sErrDescrp As String
Let lErrNum = Err.Number
Let sErrDescrp = Err.Description
Resume TraceBack
TraceBack:
On Error GoTo 0
Call RaiseError(r_lSubID:=1, r_lErrNum:=lErrNum,
r_sErrDescrp:=sErrDescrp)
Finally:
Exit Sub
End Sub

The following subroutine (found in a UserForm object named `frmFoo')
creates an instance of `clsFoo':

Public Sub Foo_Click()
Dim foo As clsFoo
On Error GoTo ErrorHandler
'...
Set foo = new clsFoo
'...
Goto Finally
ErrorHandler:
Dim lErrNum As Long
Dim sErrDescrp As String
Let lErrNum = Err.Number
Let sErrDescrp = Err.Description
Resume TraceBack
TraceBack:
On Error GoTo 0
Call RaiseError(r_lSubID:=2, r_lErrNum:=lErrNum,
r_sErrDescrp:=sErrDescrp)
Finally:
Exit Sub
End Sub

The following subroutine (found in a public module named `modFoo')
creates an instance of the `frmFoo' UserForm object:

Public Sub Foo3()
Dim foo as UserForm1
On Error GoTo ErrorHandler
'...
Set foo = new UserForm1
Call foo.Show()
'...
Goto Finally
ErrorHandler:
Resume TraceBack
TraceBack:
On Error GoTo 0
Call MsgBox(prompt:=Err.Description, Buttons:=vbCritical)
Finally:
Exit Sub
End Sub
 
M

Manfred F

Hi Trevor,
Why does the error raised in the UserForm object not propagate back to
the subroutine in the public module (i.e. the MsgBox prompt is never
executed)?

Userforms use callback functions, which do not have a superordinate context.
So, your public module subroutine doesn't call "Foo_Click". Foo_Click is the
upmost instance and has to handle the error.

Regards,

Manfred
 
T

Trevor

Is there a superordinate context for all controls found on a UserForm
object? I would prefer all run-time exceptions generated by any
control event (or subordinate function call) to be handled by a single
"master" subroutine for the UserForm object. Does the UserForm_Error
event accomplish this? I cannot find any documentation for this event
handler.
 
M

Manfred F

Is there a superordinate context for all controls found on a UserForm
object? ...

no, there is no superordinate execution context for the event procedures of
a form or its widgets. Whenever the user triggers an event, this IS the
upmost context. For each and every event procedure you need to set up an
error handler. What you can do of course is to use subroutines for common
tasks as error depiction.

Regards,
Manfred
 

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