Solved

Access Error Log File

Posted on 2015-01-03
5
251 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
  • 3
  • 2
5 Comments
 
LVL 84

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 84
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now