Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


VBA Error logging

VBA Error logging

VBA Error logging


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.


Sub LogError(msg As String)
    Dim fileName As String, fileNo As Integer
    fileNo = FreeFile 'Get first free file number
    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?


RE: VBA Error logging

"from which form" - you can do that easily:


Private Sub btnTest_Click()
    Call LogError("My Message", Me)
End Sub

Sub LogError(msg As String, frm As Form)

MsgBox frm.Name
MsgBox frm.ActiveControl.Name

You may need to declare frm As MSAccess.Form 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 LogError

---- Andy

There is a great need for a sarcasm font.

RE: VBA Error logging

Thanks Andy. That's a start.
Appreciate the suggestion.


RE: VBA Error logging

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) 

RE: VBA Error logging


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,


Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close