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

时间:2017-08-17 19:45:11

标签: vba drop-down-menu combobox access-vba audit-logging

我看过这篇文章:Saving changes to a multivalued ComboBox via AuditTrail

并尝试将花絮放入我的代码中,但它只是没有用!我不是很擅长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

1 个答案:

答案 0 :(得分:0)

您无法使用.Value.OldValue获取多值字段的值。这些属性始终返回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