By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,286 Members | 1,720 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,286 IT Pros & Developers. It's quick & easy.

Re-use values of err.number and err.description after a msgbox in the error handler

P: n/a
What I'm trying to do is to re-use the values of err.number and
err.description after displaying them in a MsgBox. The values seem to
auto clear and not be re-usable on a second reference.

For example - if I construct this:

Function ThisFunctionGeneratesAnError()
On Error GoTo ErrorHandler

'code that generates an error

ExitErrorHandler:
Exit Function
ErrorHandler:
msgox err.number & " - " & err.description
call SomeOtherFunction(err.number, err.description)
Resume ExitErrorHandler
End Function

What happens is the message box displays correctly with:
"1234 - Error message text"
but the function SomeOtherFunction is passed the values:
SomeOtherFunction(0,"")

If I reverse the order of the code to:

call SomeOtherFunction(err.number, err.description)
msgox err.number & " - " & err.description

What happens is the function SomeOtherFunction is passed the values
correctly:
SomeOtherFunction(1234,"Error message text")
but the message box appears as:
"0"

Is there a way to avoid this behaviour, without having to construct a
mess that looks like:

....
ErrorHandler:
Dim myErrNumber as long, myErrorText as String
myErrNumber=err.number
myErrorText=err.description
msgox myErrNumber & " - " & myErrorText
call SomeOtherFunction(myErrNumber, myErrorText)
Resume ExitErrorHandler
End Function

Thanks,
lq

Nov 13 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Many kinds for VBA statement clear the Err object. The answer is pretty
simple, though - copy the values to variables immediately, then use the
variables.

Personally, I like to create a function called something like ErrCapture that
takes the Err object as a parameter, copies the Err properties to properties
of a class instance, and returns the instance. The class becomes a good
central location to put code for presenting error dialogs, writing errors to
log tables, etc.

On 5 Feb 2005 03:51:49 -0800, "laurenq uantrell" <la*************@hotmail.com>
wrote:
What I'm trying to do is to re-use the values of err.number and
err.description after displaying them in a MsgBox. The values seem to
auto clear and not be re-usable on a second reference.

For example - if I construct this:

Function ThisFunctionGeneratesAnError()
On Error GoTo ErrorHandler

'code that generates an error

ExitErrorHandler:
Exit Function
ErrorHandler:
msgox err.number & " - " & err.description
call SomeOtherFunction(err.number, err.description)
Resume ExitErrorHandler
End Function

What happens is the message box displays correctly with:
"1234 - Error message text"
but the function SomeOtherFunction is passed the values:
SomeOtherFunction(0,"")

If I reverse the order of the code to:

call SomeOtherFunction(err.number, err.description)
msgox err.number & " - " & err.description

What happens is the function SomeOtherFunction is passed the values
correctly:
SomeOtherFunction(1234,"Error message text")
but the message box appears as:
"0"

Is there a way to avoid this behaviour, without having to construct a
mess that looks like:

...
ErrorHandler:
Dim myErrNumber as long, myErrorText as String
myErrNumber=err.number
myErrorText=err.description
msgox myErrNumber & " - " & myErrorText
call SomeOtherFunction(myErrNumber, myErrorText)
Resume ExitErrorHandler
End Function

Thanks,
lq


Nov 13 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.