In your first post you said you wanted to log the execution of every query. In your last post you said only if the query raises an error. I went with the latter and wrote a simplified version. However, I made the assumption that if there is an error the whole process is terminated. If that is not the case, strMsg2 would need to be declared at the module level and concatenated in the error handler using strMsg values plus a line wrap. Then Resume Next would go to the next line after the one that raised the error and carry on with the next query. I don't know what you're doing with the integer value returned by your function so I left it in.
NOTE: you need to create your log file in the same folder as the db first. See if this helps:
VBA Code:
Function archiveProcess() as Integer
Dim strMsg As String
On Error GoTo ERR_HANDLER
DoCmd.SetWarnings False
10:
strMsg = "1a-delete-tblARData failed at " & Now()
DoCmd.OpenQuery "1a-delete-tblARData"
20:
strMsg = "1b-delete-tblCurrentAssignedDate failed at " & Now()
DoCmd.OpenQuery "1b-delete-tblCurrentAssignedDate"
30:
etc.
exitHere:
SetWarnings True
Exit Function
errHandler:
Dim fs, f
Dim fpath As String
Set fs = CreateObject("Scripting.FileSystemObject")
fpath = CurrentProject & "\NameOfYourTextFile.log" '<<can use .txt if preferred
Set f = fs.OpenTextFile(fpath, 8, True)
f.Write strMsg
f.Close
Set fs = Nothing
Set f = Nothing
Resume exitHere
End Function
A couple of observations:
- your posted code suggests you forgot to turn warnings back on - not good.
- best to not use special characters or spaces in object names. Suggest you adopt a proper naming convention going forward. See
_________________________________________________________ Naming Conventions. This document is aimed at the user who is unfamiliar with any naming convention. It is based upon personal experience and the interaction I have had with others. It is not a hard and fast rule. Why use a...
www.access-programmers.co.uk
What not to use in names
List of problem field names and reserved words in Microsoft Access and JET
allenbrowne.com
- if you have a lot of queries in this code, you could Dim strQryName and write
20:
strQryName = "1b-delete-tblCurrentAssignedDate"
strMsg = strQryName & " failed at " & Now()
DoCmd.OpenQuery strQryName
That might look worse, but you could copy and paste lines 2 and 3 umpteen times and only have to write the query name once for each call.
EDIT - forgot to say that you probably want a notification message from the error handler if there is a failure, otherwise there is nothing to indicate that the log was written to, or that there was any other sort of failure.