Err.Raise + Call Stack + UserForm

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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
.



Relevant Pages

  • Re: Background Color
    ... Public Sub SetBackColor() ... On Error GoTo Error_Handler ... Dim doc As DAO.Document ... Dim frm As Form ...
    (microsoft.public.access.forms)
  • RE: How to tile an image across a form - quickly
    ... Public Sub Draw_Pattern ... 'draws dogwaffles tiled pattern into a form or picturebox ... Dim pw As Long, ph As Long ... On Error GoTo skip ...
    (microsoft.public.vb.general.discussion)
  • Re: Macro Problems
    ... Public Sub Tester002 ... Dim SH As Worksheet ... Dim Rng2 As Range ... On Error GoTo 0 ...
    (microsoft.public.excel.programming)
  • Re: Identify Blanks; All Listed in Column AX
    ... Public Sub FindBlanks() ... Dim rngMyRange As Range ... Dim rngBlanks As Range ... On Error GoTo 0 ...
    (microsoft.public.excel.programming)
  • RE: macro timer
    ... I wouldn't use "open" for a subroutine name as VBA has it's own open ... But my code opens the two files at the same time. ... Dim arrFiles() ... Public Sub link() ...
    (microsoft.public.excel.programming)