Generic Error Handling

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a sheet that has a general error handler that is called in the highest
procedure (the event handler in this case). I want to know if there is a way
to pass either a line # or a method name or even the control (since I have
checkboxes as well) that initiated the error. My general Error handler is
below and I use that to feed the info into an e-mail for the user to send to
me.

Also if I enact error_handling somewhere else in the code, to capture a
specific error, how do I ensure the remaining code is caught by the generic
error handler.

Obviously I miss my try/catch :P Thanks for any help.

Private Sub btnAdd_Click()

On Error GoTo GEN_ERROR_HANDLER

Add_Row_To_Control

Exit Sub

GEN_ERROR_HANDLER:

Generic_Error_Handling

End Sub

'*****************************************************************************
'*Name: Generic_Error_Handling
'*Desc: Alerts user to error and safetly exists from all procedures
'*Input Parameters: NONE
'*
'*Returns: NONE
'*****************************************************************************
Sub Generic_Error_Handling()

'VAR

Dim sError As String
Dim strExplain As String

'BEGIN

'Build Error String
sError = "User: " & Environ("username") & Chr(10) & _
"An unexpected error has occured with the following properties:"
& Chr(10) & Chr(10) & _
"Description: " & Err.Description & Chr(10) & _
"Source: " '& strSource (removed until source can be ID'd)

'Alert User
strExplain = InputBox(sError & Chr(10) & Chr(10) & _
"An e-mail will be generated to be send an error report so this
issue can " & _
"be fixed.", "Describe Actions")

sError = sError & Chr(10) & _
"User Explanation: " & strExplain

'Activate the control sheet
gcwsControl.Activate

'Ensure Interactivity and Updating restored
ScreenUpdating True

'Create E-mail Alerting of problem
SendEmail "Streger, Jared M", "Error Log - " & gcwbThis.Name,
BodyText:=sError

End Sub
 
J,

I'm not sure if there's a way to pass the procedure name other than passing
it as a literal string parameter to the generic error handler. For the line
number, you can use Erl, but, you must have your code with line numbers. For
example:


Sub test()
1 On Error GoTo errhandler
2
3 Dim i As Integer
4
5 i = 3 / 0

errhandler:
Generic_Error "test", CStr(Erl)
End Sub

Sub Generic_Error(strSub As String, strLineNumber As String)
MsgBox "Error occured in " & strSub & " at line number " & strLineNumber
& vbNewLine & Err.Description
End Sub
 
Yeah I read Chip's notes on Error handling which got me this far. And I have
to avoid writing VBA code with code in my particular situation. I guess what
I can do is have a public variable that I reassign in each procedure, a
little clunky but I was hoing for something better.

Thanks!
--
*********************
J Streger
MS Office Master 2000 ed.
MS Project White Belt 2003
 
Back
Top