First routine, extracts all pertinant information, that will help, analyse and correct the error.
Then, all above data, will be appended to an "ErrorLog" table.
Then generic message will be displayed to user, expressing the fact, that an error has occured. (no need for msgBox, in every error routine).
Then a general clean-up follows, (as much as can be foreseen, without causing errors... closing objects, that aren't open etc...) Otherwise, more specific clean-up should be done, in every routine.
If ErrorTable does not exist, second routine is called, which creates the "tblErrorLog". Then, first routine, attempts to run the append query again.
Third routine, simply creates a few errors, to show functionality, of the above mentioned, two routines. And, how to call Generic error handler,from every procedure. Pay heed to colored notes, at bottom.
Hope this faciltates creating an error log for others, or at least, offers a few ideas. Good Luck!
PS, I use "Error handler builder from Zada Solutions" www.zada.com.au, to place a custom error handler, in every procedure. This 3rd party utility, has the ability, to enter each produre name as an argument, into my ErrorLog procedure. As of yet, I can't do this on my own. MZ-Tools utility, adds line numbers to every procedure in project. VERY HELPFULL, to find where error occured!!! ***When wrapper is called, from each procedure, the arguments are different depending on whether it's a Form Class, or Standard Module. Examples are Below.
___________________________________________________________ Sub ErrorLog(lngNumber As Long, _ strDesc As String, _ strSource As String, _ strProcedure As String, _ Optional strForm As String, _ Optional intLineNumber As Integer) On Error GoTo xxx
Dim dteNow As Date, strComputer As String, strUser As String, strApp As String Dim strCurrentObject As String, strModule As String, strLogOn As String, strCurrentProject As String Dim strCurrentForm As String, strActiveControl As String, strUserName As String, objModule As Object
xx: Set objModule = Nothing DoCmd.SetWarnings True DoCmd.Hourglass False DoCmd.Echo True Exit Sub xxx: If Err = -2147217865 Then 'table does not exist Call CreateErrorTable 'Create table Resume 'try again to insert, error data Else MsgBox "Unexpected error - " & Err & vbCrLf & _ Error$, vbExclamation, strCurrentProject & " - ErrorLog" Resume xx End If End Sub
___________________________________________________________ Sub CreateErrorTable() On Error GoTo xxx Dim SQL As String
xx: Application.RefreshDatabaseWindow Exit Sub xxx: If Err <> -2147217900 Then 'ByPass table already exist, error MsgBox Err & vbCrLf & Error$, , "CreateErrorTable" End If Resume xx End Sub
_________________________________________________________ Sub TryErrorTable() 'From Standard Module On Error GoTo xxx
Dim v As Integer, str As String
str = Date
v = 56098789 * 34 v = 56 / 0 Dim rec As New adodb.Recordset rec.Open "SELECT * FROM tblNothing", CurrentProject.Connection, adOpenForwardOnly, adLockReadOnly
CurrentDb.Execute "DELETE FROM tblNoWhere"
xx: If rec.State = adStateOpen Then rec.Close Set rec = Nothing Exit Sub xxx: Call ErrorLog(Err, Error$, Err.Source, "Sub; TryErrorTable",,Erl) DoEvents'remove, only used for example Resume Next 'invoke the next error'remove, only for example Resume xx' keep this End Sub
____________________________________________________ Private Sub cmdTotal_Click() 'Call From Form Class Module 10 On Error GoTo xxx 20 MsgBox 5/0 xx: 30 Exit Sub xxx: 40 Call ErrorLog(Err, Error$, Err.Source, "Sub; cmdTotal_Click", Me.Name,Erl) 50 Resume xx 60 End Sub