审计跟踪代码未获取组合框列表更改

Audit trail code not picking up combobox list changes

我看过这个post:

并尝试获取花絮并将其放入我的代码中,但它没有用!我并不擅长 SQL,但我需要完成这项工作。这是我的代码,它适用于文本框,但是有人能告诉我在组合框下拉列表更改时我需要的确切位置和内容吗?

提前致谢!!

Function LogChanges(lngID As Long, Optional strField As String = "")
    Dim dbs As DAO.Database
    Dim rst As DAO.Recordset
    Dim varOld As Variant
    Dim varNew As Variant
    Dim strFormName As String
    Dim strControlName As String


    varOld = Screen.ActiveControl.OldValue
    varNew = Screen.ActiveControl.Value
    strFormName = Screen.ActiveForm.NAME
    strControlName = Screen.ActiveControl.NAME
    Set dbs = CurrentDb()
    Set rst = dbs.TableDefs("ztblDataChanges").OpenRecordset

    With rst
        .AddNew
        !FormName = strFormName
        !ControlName = strControlName
        If strField = "" Then
            !FieldName = strControlName
        Else
            !FieldName = strField
        End If
        !RecordID = lngID
        !UserName = Environ("username")
        If Not IsNull(varOld) Then
            !OldValue = CStr(varOld)
        End If
        !NewValue = CStr(varNew)
        .Update
    End With
    'clean up
    rst.Close
    Set rst = Nothing
    dbs.Close
    Set dbs = Nothing
End Function

您无法使用 .Value.OldValue 获取多值字段的值。这些属性总是 return Null。据我所知,没有可靠的方法来获取旧值(另外,适当的审计跟踪不需要旧值,因为如果一切都得到正确审计,旧值就是之前添加的新值)。

仅保存新值时,如果将它们保存到文本字段而不是多值字段,则可以使用以下方法:

使用此函数获取所有选定项目的字符串值:

Public Function JoinMVF(MVFControl As Control, Optional Delimiter As String) As String
    Dim i As Variant
    For Each i In MVFControl.ItemsSelected
        JoinMVF = JoinMVF & MVFControl.ItemData(i) & Delimiter
    Next i
End Function

然后,将您的记录集片段调整为以下内容:

With rst
    .AddNew
    !FormName = strFormName
    !ControlName = strControlName
    If strField = "" Then
        !FieldName = strControlName
    Else
        !FieldName = strField
    End If
    !RecordID = lngID
    !UserName = Environ("username")
    If Not IsNull(varOld) Then 'varOld will always be Null for a multi-valued field
        !OldValue = CStr(varOld) 'Thus this will never get called
    End If
    'Add some If multivalued field then
    !NewValue = JoinMVF(Screen.ActiveControl, "; ")
    .Update
End With