Rijnsent
Well-known Member
- Joined
- Oct 17, 2005
- Messages
- 1,424
- Office Version
- 365
- Platform
- Windows
I've built a bit of VBA code to run an action query (append/delete/insert into/update) and to log its result, I will run this function from a MS Access macro. There are a bunch of action queries, so I want to make a general function that will log the results. RecordsAffected is one bit of that, but I'm looking for something like "TableAffected". An action query can have multiple inputs, but in the end there is only 1 table influenced by it (rows added, deleted, updated).
I've had a look at MsysQueries and the properities of a QueryDefs, but the only way to go seems to be to try to disect the SQL statement. Is there anyone here with a working solution to extract the "TableAffected" from any given action query?
Thanks!
Koen
I've had a look at MsysQueries and the properities of a QueryDefs, but the only way to go seems to be to try to disect the SQL statement. Is there anyone here with a working solution to extract the "TableAffected" from any given action query?
Thanks!
Koen
Code:
Function RunLogQuery(QueryName As String)
Dim db As Database
Set db = CurrentDb
Set qry = Nothing
On Error Resume Next
Set qry = db.QueryDefs(QueryName)
On Error GoTo 0
If qry Is Nothing Then
'No query found, log error
MsgBox "ERROR, unknown query: " & QueryName, vbOKOnly + vbCritical
Else
varReturn = SysCmd(acSysCmdSetStatus, "Running: " & QueryName)
Multi = 1
If InStr(qry.SQL, "INSERT INTO") > 0 Then
TblAffected = Trim(Mid(qry.SQL, Len("INSERT INTO") + 1, InStr(qry.SQL, "(") - Len("INSERT INTO") - 1))
ElseIf InStr(qry.SQL, "DELETE ") > 0 Then
Multi = -1
If InStr(qry.SQL, "WHERE ") > 0 Then
TblAffected = Trim(Mid(qry.SQL, InStr(qry.SQL, "FROM ") + 5, 1))
ElseIf InStr(qry.SQL, ";") > 0 Then
TblAffected = Trim(Mid(qry.SQL, InStr(qry.SQL, "FROM ") + 5, InStr(qry.SQL, ";") - InStr(qry.SQL, "FROM ") - 5))
Else
TblAffected = Trim(Mid(qry.SQL, InStr(qry.SQL, "FROM ") + 5, Len(qry.SQL) - InStr(qry.SQL, "FROM ") - 5))
End If
ElseIf InStr(qry.SQL, "UPDATE ") > 0 Then
TblAffected = Trim(Mid(qry.SQL, Len("UPDATE") + 1, InStr(Len("UPDATE ") + 1, qry.SQL, " ") - Len("UPDATE") - 1))
Else
TblAffected = "UNKNOWN"
End If
On Error GoTo Err_Execute
qry.Execute
On Error GoTo 0
Set rstLog = db.OpenRecordset("XTBL_LOG")
rstLog.AddNew
rstLog("TIMESTAMP") = Now()
rstLog("ITM") = TblAffected
rstLog("ACTION") = QueryName
rstLog("VAL") = qry.RecordsAffected * Multi
rstLog("USER") = Environ("USERNAME")
rstLog.Update
rstLog.Close
Set rstLog = Nothing
varReturn = SysCmd(acSysCmdSetStatus, " ")
End If
Set qry = Nothing
Set db = Nothing
Exit Function
Err_Execute:
' Notify user of any errors that result from
' executing the query.
If DBEngine.Errors.Count > 0 Then
For Each errLoop In DBEngine.Errors
MsgBox QueryName & vbCr & "Error number: " & errLoop.Number & vbCr & errLoop.Description, vbCritical + vbOKOnly
Next errLoop
End If
varReturn = SysCmd(acSysCmdSetStatus, " ")
Set qry = Nothing
Set db = Nothing
End Function