我看过这篇文章:Saving changes to a multivalued ComboBox via AuditTrail
并试图将花边新闻放入我的代码中,但它就是不起作用!我真的不是很擅长SQL,但我需要完成这项工作。这是我的代码,它适用于文本框,但有人能告诉我,我的combobox下拉列表更改需要放在哪里和确切的位置吗?
提前感谢!!
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发布于 2017-08-21 17:34:41
不能使用.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 Withhttps://stackoverflow.com/questions/45743788
复制相似问题