Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBA Error logging 2

Status
Not open for further replies.

VicM

Programmer
Sep 24, 2001
444
US
Hi,

I'm trying to polish my MSAccess VBA code by introducing error logging. I discovered a way to log the errors to an external file. The example showed how to pass the error message to a Sub which logs the info to a file. Obviously I can also pass the error number as well if I choose. The following is the code I found which is called from each 'On Error' handler section of the Sub or Function.

Code:
Sub LogError(msg As String)
    Dim fileName As String, fileNo As Integer
    fileNo = FreeFile [COLOR=#4E9A06]'Get first free file number[/color]
    fileName = "f:\DBMS\error_log.txt"
    Open fileName For Append As #fileNo
    Print #fileNo, Now & ":" & msg
    Close #fileNo
End Sub

But I'm at a loss as to how to pass to the error log, the source of the error. By that I mean which Sub or Function, from which form/report/module caused the error.

Is there a way to extract the Sub/Function/Module name source so I can pass it to the logging Sub?

Thanks,
Vic
 
from which form" - you can do that easily:

Code:
Private Sub btnTest_Click()
    Call LogError("My Message", [blue]Me[/blue])
End Sub

Sub LogError(msg As String, [blue]frm As Form[/blue])

MsgBox frm.Name
MsgBox frm.ActiveControl.Name
...

You may need to declare [tt]frm As MSAccess.Form[/tt] or something like that.

As far as "Sub/Function/Module name source " - I don't know how to do that, unless you would pass another specific (hard-coded) parameter to [tt]LogError[/tt]


---- Andy

There is a great need for a sarcasm font.
 
Thanks Andy. That's a start.
Appreciate the suggestion.

Vic
 
No generic way to do it, but since you have to call the error log procedure you just pass it in. Just copy and paste the code into your errorhandler and change the sub routine name.

..... in your error handler paste the following and change the routine name
Code:
dim msg as string
dim rtnName as string
rtnName = "Type Your Routine Name Here"
msg = err.number & ": " & Err.description & " in routine " & rtnName  & " on form/report: " & me.name
Call LogError(msg)
 
MajP

I like the way you think. I was doing something along those lines at the class level initially.
I set a Private Const at the class level with the Class/Module name. Then when I called the ErrorLog routine, I passed the Const as one of the arguments. I was considering doing exactly what you suggested at the Sub/Function level as well.

It's not the most elegant solution, but it will work and be helpful to track where an error occurs.
So a star for you and one for Andy who got me thinking along those lines as well.

Thanks folks,

Vic
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top