Skip to content

Instantly share code, notes, and snippets.

@mrowles
Last active October 13, 2015 22:08
Show Gist options
  • Save mrowles/4263168 to your computer and use it in GitHub Desktop.
Save mrowles/4263168 to your computer and use it in GitHub Desktop.
Microsoft Access: Central Error Handler
'Global Error Varibles
Public bDisplayError As Boolean
Public sProcName, sDetails As String
Public iErrorCount As Integer
Public Function bCentralErrorHandler(ByVal sModule$, ByVal sSub$, ByVal sDetails$) As Boolean
'Make sure there is no errors in the Error Handling system
On Error Resume Next
Dim sFullSource, sPath, sFile, sUsername As String
'Get username of current user
sUsername = Environ("USERNAME")
'Get directory of current file
sFile = Left(CurrentDb.NAME, Len(CurrentDb.NAME) - Len(Dir(CurrentDb.NAME)))
'Get the file the error came from, the module, and a procedure description
sFullSource = "[" & sFile & "]" & " Module=" & sModule & " Procedure=" & sSub & " "
'Log the error
'Please update [PROJECTNAME]
Open sFile & "PROJECTNAME Runtime Error - " & sUsername & ".log" For Append As #1
Print #1, sUsername & " | " & sFullSource & Format$(Now(), " dd/mm/yyyy hh:mm AM/PM") & vbCrLf & " " & sDetails
Close #1
'MsgBox sDetails
iErrorCount = iErrorCount + 1
'Clear errors
Err.Clear
'Reset the error handler
bCentralErrorHandler = False
End Function
'Using the Central Error Handler
'Please update [MODULENAME] & [SUB/FUNCTIONNAME]
Sub SubExample
On Error GoTo ErrHandler
'Sub content here
'Exit the sub at the end so ErrHandler isn't run unnecessarily
Exit Sub
ErrHandler:
sDetails = "Error Number = " & CStr(Err.Number) & " Error Description = " & Err.Description
If bCentralErrorHandler("MODULENAME", "SUB/FUNCTIONNAME", sDetails) Then
End If
Resume Next
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment