0

我正在尝试在发票表单上运行审计跟踪模块以跟踪更改。相同的模块适用于我的其他几个表单,当我没有运行 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

对我有什么想法吗?蒂亚!!

4

1 回答 1

0

您的 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 & ", " _
...

这是草率的代码,但你明白了。您应该对插入到 Audit Trail 中的所有字符串值执行此操作。

于 2019-02-22T19:03:24.610 回答