我正在尝试在Invoice表单上运行Audit Trail模块以跟踪更改。相同的模块可以与我的其他几种表单一起使用,当我没有运行Audit Trail时,我的Invoice表单不会导致任何错误。我不是专家,不知道如何解决这个问题!这是表单的记录源的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;
这是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
对我来说有什么想法? TIA!
您的Audit Trail代码具有天真的类型转换。只需添加“RecordSource字符串两端的字符”就不会一直有效。
当前(错误)引用的RecordSource值:
"SELECT ... , tblPeople.[Firstname] & " " & [Lastname] AS FullName, ... "
问题是中间的" "
把它变成了两个字符串!
正确的Access SQL值:
"SELECT ... , tblPeople.[Firstname] & "" "" & [Lastname] AS FullName, ... "
现在引用引号,插入将正常工作。
要修复代码,请执行以下操作:
...
& cDQ & Replace(frm.RecordSource, cDQ, cDQ & cDQ) & cDQ & ", " _
...
这是邋code的代码,但你明白了。您应该对要插入Audit Trail的所有字符串值执行此操作。