Proper way to pass errors up the line

T

Tod

Whenever I post these questions I wonder what it's going
to look like to a Google user five years from now. So:

"Hello from the past!"

Now here is my VBA newbie question o' the day:

How do I pass a raised error back to the procedure that
called?

Example (Don't laugh):

Sub FirstProc()
'Code Code Code
On Error Resume Next
Call Second
If Err.Number = vbObjectError + 513 Then
msgbox "This error came from SecondProc"
End if
'Code Code Code
End First

Sub SecondProc()
'Code Code Code
Err.Raise Number:=vbObjectError + 513, Description:="I
raised this error."
End Sub

This is obviously not correct, but hopefully you see what
I'm trying to do. I want SecondProc to pass my error back
to FirstProc and end without causing an error while in
SecondProc. Then when FirstProc gets the error it displays
a message or does something based on the err.number being
my raised error.

If I'm on the right track please show me where it's not
working. If I'm waaaay off please tell me how to do it.

tod
 
H

Henry

Tod,

One way, probably not the most elegant, that will work is to set up global
variables in the module called, say "GlblMyError" and "GlblErrDesc"
Before calling Second set these to 0 and ""

GlblMyError = 0
GlblErrDesc =""
In Second, if an error occurs, set GlblMyError = Error No. and GlblErrDesc
=Error description.

After returning from Second, check the value of GlblMyError

If GlblMyError =0 Then 'No error

'do the rest of your code

Else 'an error has occurred
Msgbox "An error has occurred in SecondProc. Error Code is " & GlblMyError *
"Description is " & GlblErrDesc

HTH
Henry
 

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