Passing Err Object

D

DBG

I seem to be having trouble passing the err object to a function, I'm sure I
just have no idea what object type I'm supposed to be receiving but this one
is eluding me.

can someone fill in the ??? below :)

'some sub

'error handler
report_error(err)

'end sub

function report_error(errObj as ???)

msgbox errObj.Description
msgbox errObj.Number

end function
 
B

Brendan Reynolds

There's usually no need to pass it. It's global, so the function can access
it directly ...

'error handler
report_error

Function report_error
MsgBox "Error " & Err.Number & ": " & Err.Description
End Function

When you do need to store the values, a custom class can be used ...

'In a class module saved as clsSavedError ...

Option Compare Database
Option Explicit

Private mlngErrorNumber As Long
Private mstrErrorDescription As String

Public Property Let ErrorNumber(lngErrorNumber As Long)
mlngErrorNumber = lngErrorNumber
End Property

Public Property Get ErrorNumber() As Long
ErrorNumber = mlngErrorNumber
End Property

Public Property Let ErrorDescription(strErrorDescription As String)
mstrErrorDescription = strErrorDescription
End Property

Public Property Get ErrorDescription() As String
ErrorDescription = mstrErrorDescription
End Property

Private Sub Class_Initialize()
mlngErrorNumber = Err.Number
mstrErrorDescription = Err.Description
End Sub

'In a standard module ...

Option Compare Database
Option Explicit

Public Sub TestSavedError()

Dim SavedError As clsSavedError

On Error GoTo ErrorHandler
Err.Raise 11

ExitProcedure:
Exit Sub

ErrorHandler:
Set SavedError = New clsSavedError
ShowError SavedError
Resume ExitProcedure

End Sub

Public Sub ShowError(SavedError As clsSavedError)
MsgBox "Error " & SavedError.ErrorNumber & ": " &
SavedError.ErrorDescription
End Sub
 
D

DBG

cool thanks!

Brendan Reynolds said:
There's usually no need to pass it. It's global, so the function can access
it directly ...

'error handler
report_error

Function report_error
MsgBox "Error " & Err.Number & ": " & Err.Description
End Function

When you do need to store the values, a custom class can be used ...

'In a class module saved as clsSavedError ...

Option Compare Database
Option Explicit

Private mlngErrorNumber As Long
Private mstrErrorDescription As String

Public Property Let ErrorNumber(lngErrorNumber As Long)
mlngErrorNumber = lngErrorNumber
End Property

Public Property Get ErrorNumber() As Long
ErrorNumber = mlngErrorNumber
End Property

Public Property Let ErrorDescription(strErrorDescription As String)
mstrErrorDescription = strErrorDescription
End Property

Public Property Get ErrorDescription() As String
ErrorDescription = mstrErrorDescription
End Property

Private Sub Class_Initialize()
mlngErrorNumber = Err.Number
mstrErrorDescription = Err.Description
End Sub

'In a standard module ...

Option Compare Database
Option Explicit

Public Sub TestSavedError()

Dim SavedError As clsSavedError

On Error GoTo ErrorHandler
Err.Raise 11

ExitProcedure:
Exit Sub

ErrorHandler:
Set SavedError = New clsSavedError
ShowError SavedError
Resume ExitProcedure

End Sub

Public Sub ShowError(SavedError As clsSavedError)
MsgBox "Error " & SavedError.ErrorNumber & ": " &
SavedError.ErrorDescription
End Sub
 
Top