Folks,
I’m new at VBA programming, but I’m trying to do things right. Hence error handling. Maybe I’m obsessive, but I find that a lot of my routines are taken up by repetitive error handling code. My error handler looks like this:
Private Sub procErrorHandler( _
ByRef intErrorNumber As Integer, _
ByRef strErrorDescription As String, _
ByRef strUserError As String, _
ByRef blnErrorFlag As Boolean _
)
If intErrorNumber 0 Then
MsgBox “Runtime Error Number ” & intErrorNumber & ” occurred. ” & _
“Description: ” & strErrorDescription & “.”, _
vbOKOnly, “Error:”
End If
If strUserError “” Then
blnErrorFlag = True
MsgBox strUserError, vbOKOnly, “Error”
End If
In the calling routine I do the following:
On Error GoTo ErrorHandler
‘Set up error-related variables.
intErrorNumber = 0
strErrorDescription = “”
strUserError = “”
blnErrorFlag = False
ErrorHandler:
intErrorNumber = Err.Number
strErrorDescription = Err.Description
Call procErrorHandler(intErrorNumber, strErrorDescription, strUserError, blnErrorFlag)
GoTo LeaveFunction
LeaveFunction:
Exit Function
End Sub
This is a lot of overhead, since I do it for each procedure. Also, it makes the code messy, obscuring the real work the procedure does. Surely someone has come up with a better mousetrap. But although I have looked (really), I haven't found it.
I imagine, by the way, that I ought to declare ErrorHandler Public and maybe it and some of my variables so that they are less generic.
TIA