I'm rather fond of the method of recreating the stack that the second article suggests. I've done that now for several projects I'm working on. Not a little amount of work either. I thought I'd share something I've added to it. By just pushing the procedure name onto the stack, you don't get the module name or project. Not a big deal for something that's small, but if you're spanning several projects, that could get confusing.
So, I've created constantsto capture the project name and module name, then a little function to concatenate them with the procedure name to push to the stack recreator. My default procedure now looks something like this...
[VBA] Sub RoutineName()
'Comments: What this routine does and how to use it
If gcfHandleErrors Then On Error GoTo RoutineName_Error
PushCallStack ConcatenateProjectNameAndModuleNameAndProcedureName( _
gcstrProjectName, _
mcstrModuleName, _
"RoutineName")
' << Your code here >>
RoutineName_Exit:
PopCallStack
Exit Sub
RoutineName_Error:
Select Case Err.Number
'Local Error Handling
Case Else
GlobalErrHandler
End Select
Resume RoutineName_Exit
End Sub
[/VBA]
And here's the function I'm currently using...
[VBA] Function ConcatenateProjectNameAndModuleNameAndProcedureName( _
ProjectName As String, _
ModuleName As String, _
ProcedureName As String)
' This function should be called by every procedure with error handling to
' concatenate ProjectName.ModuleName.ProcedureName to use for the log
' called by the global error handler. A public constant should be declared
' to house the project name, and a module level constant should be used
' to house individual module names to reduce the amount of customization
' required per procedure.
If gcfHandleErrors Then On Error GoTo _
ConcatenateProjectNameAndModuleNameAndProcedureName_Error
PushCallStack gcstrProjectName _
& mcstrModuleName _
& ".ConcatenateProjectNameAndModuleNameAndProcedureName"
ConcatenateProjectNameAndModuleNameAndProcedureName = ProjectName _ & "." & ModuleName _
& "." & ProcedureName
ConcatenateProjectNameAndModuleNameAndProcedureName_Exit:
PopCallStack
Exit Function
ConcatenateProjectNameAndModuleNameAndProcedureName_Error:
Select Case Err.Number
Case Else
GlobalErrHandler
Resume ConcatenateProjectNameAndModuleNameAndProcedureName_Exit
End Select
End Function [/VBA]
Log sample...
Error Number: 11
Error Description: Division by zero
CallStack (0): TestLogError
Date/Time: 6/17/2005 7:32:40 PM
User: Marco
Error Number: -2147467259
Error Description: [Microsoft][ODBC Excel Driver] '(unknown)' is not a valid
path. Make sure that the path name is spelled correctly and that you are
connected to the server on which the file resides.
CallStack (0): RealEstateStartup.modDataMerge.MergeTemplateWithData
CallStack (1): RealEstateStartup.modLoadConfiguration.LoadFileExtenions
CallStack (2): CodeLibrary.modAdoOperations.ReadDataFromWorkbook
Date/Time: 6/17/2005 8:03:48 PM
User: Marco
Error Number: 76
Error Description: Path not found
CallStack (0): RealEstateStartup.modDataMerge.MergeTemplateWithData
CallStack (1): CodeLibrary.modFTP.GetFile
Date/Time: 6/17/2005 8:41:18 PM
User: Marco
I'm planning to add a few new things to the log:
- Message sent to user
- My own error description (in this case intended to be used to troubleshoot problems by employees of my client after my work for them is done)
- Line number that generated error (don't have them in the code right now)