error.number

  • Thread starter Thread starter T. Alksndr Rstrpo Prado
  • Start date Start date
T

T. Alksndr Rstrpo Prado

Hi,
I got this code on a Module:

Sub FncError(TxtName As String)
On Error GoTo Err_FnceRRor
Beep
MsgBox Err.Number & " " & Err.Description & Chr
(13) & TxtName, vbCritical, "Error"
Exit Sub
Err_FnceRRor:
Beep
MsgBox Err.Number & " " & Err.Description, vbCritical
End Sub

when I used on a function but it doesn't work, when the
error occurs, the msgbox xhow error 0, and the error
number it's other.
What's wrong?
thanks for the help.
Be good
the force be with you
 
What is this? An error handler for an error handler?
You're getting a message box with an error # of zero
because there is no error. All I can offer is an example;

Sub RunMe()
On Error GoTo Err_Handler
'insert your code here
Exit Sub
Err_Handler:
Select Case Err
Case 0: MsgBox "No Error",vbInformation
Case Else: MsgBox Err.Description, vbCritical, _
"Error #" & Err.Number
Stop
End Select
End Sub
 
T. Alksndr Rstrpo Prado said:
I got this code on a Module:

Sub FncError(TxtName As String)
On Error GoTo Err_FnceRRor
Beep
MsgBox Err.Number & " " & Err.Description & Chr
(13) & TxtName, vbCritical, "Error"
Exit Sub
Err_FnceRRor:
Beep
MsgBox Err.Number & " " & Err.Description, vbCritical
End Sub

when I used on a function but it doesn't work, when the
error occurs, the msgbox xhow error 0, and the error
number it's other.

Are you calling this procedure to report an error in some
other procedure? If so, you must copy the Err.Number value
to a variable immediately after the initial error because it
may be reset very quickly. If another line of code does not
have an error, then the error number is set to 0, which is
what you're seeing.

I would have to see the call to the above function in
context to know how it could be handled, but, in general,
you will probably want to pass the error number as an
argument to the above function.

Sub FncError(TxtName As String, _
ErrNum As Long, _
ErrDescr As String)
On Error GoTo Err_FnceRRor
Beep
MsgBox ErrNum & " " & ErrDescr & _
Chr(13) & Chr(10) & TxtName, vbCritical, "Error"
. . .

Note that a new line in Access in specified by a CR followed
by a LF. In VBA code, you can use the built in constant
vbCrLf instead of the combination of Chr functions.
 
T. Alksndr Rstrpo Prado said:
Hi,
I got this code on a Module:

Sub FncError(TxtName As String)
On Error GoTo Err_FnceRRor
Beep
MsgBox Err.Number & " " & Err.Description & Chr
(13) & TxtName, vbCritical, "Error"
Exit Sub
Err_FnceRRor:
Beep
MsgBox Err.Number & " " & Err.Description, vbCritical
End Sub

when I used on a function but it doesn't work, when the
error occurs, the msgbox xhow error 0, and the error
number it's other.
What's wrong?
thanks for the help.
Be good
the force be with you

If you're calling this procedure from some other routine's
error-handler, I believe you're going to have to pass the Err.Number and
Err.Description values to the routine, like this:

'----- revised subroutine definition -----
Sub subHandleError(ErrNum As Long, ErrDesc As String, TxtName As String)

On Error GoTo Err_subHandleError

DoCmd.Beep
MsgBox _
ErrNum & " " & ErrDesc & Chr(13) & TxtName, _
vbCritical, "Error"

Exit_subHandleError:
Exit Sub

Err_subHandleError:
Beep
MsgBox Err.Number & " " & Err.Description, vbCritical
Resume Exit_subHandleError

End Sub
'----- end subroutine definition -----

'----- sample calling routine -----
Sub Test_subHandleError()

On Error GoTo Err_Handler

' ... do something that raises an error ...
Err.Raise vbObjectError + 1, , "This is my error"

Exit_Here:
Exit Sub

Err_Handler:
subHandleError Err.Number, Err.Description, "Called from
Test_subHandleError"
Resume Exit_Here

End Sub
'----- end sample calling routine -----
 
Marshall Barton said:
Note that a new line in Access in specified by a CR followed
by a LF. In VBA code, you can use the built in constant
vbCrLf instead of the combination of Chr functions.

Actually, Marsh, with the MsgBox function a new line can be achieved by
a CR alone. So a line like this:

MsgBox "Line 1" & Chr(13) & "Line 2"

or this:

MsgBox "Line 1" & vbCr & "Line 2"

will display two lines in the message box.
 
Dirk said:
Actually, Marsh, with the MsgBox function a new line can be achieved by
a CR alone. So a line like this:

MsgBox "Line 1" & Chr(13) & "Line 2"

or this:

MsgBox "Line 1" & vbCr & "Line 2"

will display two lines in the message box.


So much for consistancy, but thanks for the clarification.
I've never tried that before (and probably never again
either ;-)
 
Hi,
thanks all, I get it.
thanks a lot
I will pass the error number and description as part to
the function.
Bye
thanks
the force be with you
 
Back
Top