Solved

Access Error Log File

Posted on 2015-01-03
5
264 Views
Last Modified: 2015-01-03
Hello Experts
I am attempting to add an Error Logging function to my code.  I found a bit of code from an Allan Browne but I'm not set that this would have to be the code.  

I tried testing it to make sure I understood what it was doing.  Then I tried modifying it just a bit to add a line that would get the Form Name and then also to get the Active Control Name so I could use it generically.

On the Function, the only thing I changed was Line 2 I added strErrorForm As String  ...... THEN
I added Line 37          rst![ErrFormName] = Left$(strErrForm, 255)

The Function is
Function LogError(ByVal lngErrNumber As Long, ByVal strErrDescription As String, _
    strErrForm As String, strCallingProc As String, Optional vParameters, Optional bShowUser As Boolean = True) As Boolean
On Error GoTo Err_LogError
    ' Purpose: Generic error handler.
    ' Logs errors to table "tLogError".
    ' Arguments: lngErrNumber - value of Err.Number
    ' strErrDescription - value of Err.Description
    ' strCallingProc - name of sub|function that generated the error.
    ' vParameters - optional string: List of parameters to record.
    ' bShowUser - optional boolean: If False, suppresses display.
    ' Author: Allen Browne, allen@allenbrowne.com

    Dim strMsg As String      ' String for display in MsgBox
    Dim rst As DAO.Recordset  ' The tLogError table

    Select Case lngErrNumber
    Case 0
        Debug.Print strCallingProc & " called error 0."
    Case 2501                ' Cancelled
        'Do nothing.
    Case 3314, 2101, 2115    ' Can't save.
        If bShowUser Then
            strMsg = "Record cannot be saved at this time." & vbCrLf & _
                "Complete the entry, or press <Esc> to undo."
            MsgBox strMsg, vbExclamation, strCallingProc
        End If
    Case Else
        If bShowUser Then
            strMsg = "Error " & lngErrNumber & ": " & strErrDescription
            MsgBox strMsg, vbExclamation, strCallingProc
        End If
'        Set rst = CurrentDb.OpenRecordset("tLogError", , dbAppendOnly)
        Set rst = CurrentDb.OpenRecordset("xtmp_ErrorRptg", , dbAppendOnly) 'Name changed by WLW my LVL Project
        rst.AddNew
            rst![ErrNumber] = lngErrNumber
            rst![ErrDescription] = Left$(strErrDescription, 255)
            rst![ErrFormName] = Left$(strErrForm, 255)
            rst![ErrDate] = Now()
            rst![CallingProc] = strCallingProc
            rst![UserName] = CurrentUser()
            rst![ShowUser] = bShowUser
            If Not IsMissing(vParameters) Then
                rst![Parameters] = Left(vParameters, 255)
            End If
        rst.Update
        rst.Close
        LogError = True
    End Select

Exit_LogError:
    Set rst = Nothing
    Exit Function

Err_LogError:
    strMsg = "An unexpected situation arose in your program." & vbCrLf & _
        "Please write down the following details:" & vbCrLf & vbCrLf & _
        "Calling Proc: " & strCallingProc & vbCrLf & _
        "Error Number " & lngErrNumber & vbCrLf & strErrDescription & vbCrLf & vbCrLf & _
        "Unable to record because Error " & Err.Number & vbCrLf & Err.Description
    MsgBox strMsg, vbCritical, "LogError()"
    Resume Exit_LogError
End Function

Open in new window


On the Click event of My form I added this Code
I modified the Suggested original String on LINE 18
Originally it was             Call LogError(Err.Number, Err.Description, "Command10_Click()")
But using that I didn't get the Form Name and I have to change the "Command10_Click()") for every place I use it.

Private Sub Command10_Click()

Dim strErrForm As String, strCtl As String

On Error GoTo Err_Proc:

DoCmd.Save

Err_Proc:  ' Label to jump to on error.
    Select Case Err.Number
'        Case 9999                        ' Whatever number you anticipate.
'            Resume Next                  ' Use this to just ignore the line.
        Case 999
            Resume Exit_ErrProc         ' Use this to give up on the proc.
        Case Else                        ' Any unexpected error.
            strCtl = Me.ActiveControl.Name
            strErrForm = Me.Form.Name
            Call LogError(Err.Number, Err.Description, "" & strErrForm & "','" & strCtl & "'()")
            Resume Exit_ErrProc ' Pick up again and quit.
    End Select

Exit_ErrProc: ' Label to resume after error.
     Exit Sub ' Exit before error handler.

End Sub

Open in new window


NOW WHEN I TRY TO COMPILE it errors out in the Compile on that line Call LogError
It tells me it is not Optional


Any suggestions or is there a better way to log an error table so I can track errors.?

Oh... ANOTHER ODDITY that I don't understand.... If I have the Exit_ErrProc: before the Err_Proc: coding when I encounter the error it never gets to the Err_Proc..... it processes the Exit_ErrProc ?????? Don't understand why but that's what happens

PS.... This was just a testing so I do rename the Command buttons... just not in this testing case.
0
Comment
Question by:wlwebb
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 85

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 500 total points
ID: 40529063
The error is essentially telling you that you haven't provided all the required parameters. You're issuing this call:

Call LogError(Err.Number, Err.Description, "" & strErrForm & "','" & strCtl & "'()")

Which essentially concats your strErrForm and strCtl values together, so you're essentially issuing the call with 3 arguments, instead of the required 4. You should be able to call it like this:

Call LogError(Err.Number, Err.Description, Me.Form.Name, Screen.ActiveControl.Name)

The last two items in that call are optional, and you don't need to include them (unless you want to). If you do, then you just include them like this:

Call LogError(Err.Number, Err.Description, Me.Form.Name, Screen.ActiveControl.Name, "1,2,3,4", True)

Oh... ANOTHER ODDITY that I don't understand.... If I have the Exit_ErrProc: before the Err_Proc: coding when I encounter the error it never gets to the Err_Proc..... it processes the Exit_ErrProc ?????? Don't understand why but that's what happens
Assuming you've got the "On Error GoTo" line in the right place, that should not happen. Try to Compile your code (from the vba menu, click debug - compile) and see what happens.
0
 

Author Comment

by:wlwebb
ID: 40529079
I have the On Error Goto Err_Proc: as the first line of code after any Dim statements....???????????


To test it I have a dummy table that I'm trying to save to but the info to save I've force to error because a field isn't filled out that is required...
Private Sub Command10_Click()

On Error GoTo Err_Proc:

DoCmd.Save

Exit_ErrProc: ' Label to resume after error.
     Exit Sub ' Exit before error handler.

Err_Proc:  ' Label to jump to on error.
    Select Case Err.Number
'        Case 9999                        ' Whatever number you anticipate.
'            Resume Next                  ' Use this to just ignore the line.
        Case 999
            Resume Exit_ErrProc         ' Use this to give up on the proc.
        Case Else                        ' Any unexpected error.
            Call LogError(Err.Number, Err.Description, Me.Form.Name, Screen.ActiveControl.Name)
'            Call LogError(Err.Number, Err.Description, "" & strErrForm & "','" & strCtl & "'()")
            Resume Exit_ErrProc ' Pick up again and quit.
    End Select

End Sub

Open in new window

0
 

Author Comment

by:wlwebb
ID: 40529086
Notice that it is Logging an Error Code 20 which is "Resume without Error...

So makes me wonder... does this Error Logging code execute every time whether there's an error or not...???  It's logging that Command11 code which Doesn't do a thing other than test if it will run ... no actions.  BUT it logs and error 20
0
 

Author Closing Comment

by:wlwebb
ID: 40529088
HOLD ON... I'm Going to make this a new question... You answered the primary question here...  THANK YOU
0
 
LVL 85
ID: 40529144
So makes me wonder... does this Error Logging code execute every time whether there's an error or not...???  It's logging that Command11 code which Doesn't do a thing other than test if it will run ... no actions.  BUT it logs and error 20
In your case, it would log any error other than 999 or 9999. It would not execute unless there is an error, since the exit routine above that would force the execution to stop.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question