[英]Syntax error (missing operator) in query expression when running Audit Trail module in MS Access
I am trying to run an Audit Trail module on an Invoice form to keep track of changes. 我正在尝试在发票表单上运行审核跟踪模块以跟踪更改。 The same module works fine with several of my other forms, and my Invoice form doesn't cause any errors when I don't have the Audit Trail running. 同一模块可以与其他几种表单配合使用,并且当我没有运行审计追踪时,发票表单也不会引起任何错误。 I am no expert and am not sure what to do to solve this! 我不是专家,也不确定该怎么做! Here is the SQL for the form's record source, which is what causes the error: 这是表单的记录源的SQL,这是导致错误的原因:
SELECT tblInvoice.*, tblAssignment.RateOut, tblTaskOrder.TaskOrderID, tblTaskOrder.TaskOrderName, tblPeople.PeopleID, tblPeople.[Firstname] & " " & [Lastname] AS FullName, tblVendor.VendorName
FROM (((tblInvoice INNER JOIN tblAssignment ON tblInvoice.AssignmentID = tblAssignment.AssignmentID) INNER JOIN tblTaskOrder ON tblAssignment.TaskOrderID = tblTaskOrder.TaskOrderID) INNER JOIN tblPeople ON tblAssignment.PeopleID = tblPeople.PeopleID) INNER JOIN tblVendor ON tblPeople.Vendor = tblVendor.VendorID;
And here is the Audit Trail module code: 这是Audit Trail模块代码:
Option Compare Database
Option Explicit
Const cDQ As String = """"
Sub AuditTrail(frm As Form, recordid As Control)
'Track changes to data.
'recordid identifies the pk field's corresponding
'control in frm, in order to id record.
Dim ctl As Control
Dim varBefore As Variant
Dim varAfter As Variant
Dim strControlName As String
Dim strSQL As String
On Error GoTo ErrHandler
'Get changed values.
For Each ctl In frm.Controls
With ctl
'Avoid labels and other controls with Value property.
If .ControlType = acTextBox Then
If .Value <> .OldValue Then
varBefore = .OldValue
varAfter = .Value
strControlName = .Name
'Build INSERT INTO statement.
strSQL = "INSERT INTO " _
& "tblAudit (EditDate, RecordID, SourceTable, " _
& " SourceField, BeforeValue, AfterValue) " _
& "VALUES (Now()," _
& cDQ & recordid.Value & cDQ & ", " _
& cDQ & frm.RecordSource & cDQ & ", " _
& cDQ & .Name & cDQ & ", " _
& cDQ & varBefore & cDQ & ", " _
& cDQ & varAfter & cDQ & ")"
'View evaluated statement in Immediate window.
Debug.Print strSQL
DoCmd.SetWarnings False
DoCmd.RunSQL strSQL
DoCmd.SetWarnings True
End If
End If
End With
Next
Set ctl = Nothing
Exit Sub
ErrHandler:
MsgBox Err.Description & vbNewLine _
& Err.Number, vbOKOnly, "Error"
End Sub
Any ideas for me? 对我有什么想法吗? TIA!! TIA!
Your Audit Trail code has naive type conversion. 您的审计跟踪代码具有幼稚的类型转换。 Just tacking " characters on either end of the RecordSource string won't work all the time. 只是套结“的记录源串的两端字符将无法正常工作的所有时间。
Current (wrong) quoted RecordSource value: 当前(错误)引用的RecordSource值:
"SELECT ... , tblPeople.[Firstname] & " " & [Lastname] AS FullName, ... "
The problem is the " "
in the middle turns it into two strings! 问题是中间的" "
将其变成两个字符串!
Correct value for Access SQL: Access SQL的正确值:
"SELECT ... , tblPeople.[Firstname] & "" "" & [Lastname] AS FullName, ... "
Now that the quotes are quoted, the insert will work fine. 现在引用已被引用,插入将正常工作。
To fix your code, do something like: 要修复您的代码,请执行以下操作:
...
& cDQ & Replace(frm.RecordSource, cDQ, cDQ & cDQ) & cDQ & ", " _
...
This is sloppy code, but you get the idea. 这是草率的代码,但是您明白了。 You should do this for all string values you are inserting into the Audit Trail. 您应该对要插入到“审计跟踪”中的所有字符串值执行此操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.