我目前正在为MS-Access 2010数据库添加审计跟踪,我正在努力处理
"错误3251:此类型对象不支持操作"
这是我的审计跟踪模块的代码,大部分是来自web的代码:
Public Function auditChanges(RecordID As String, userAction As String, cForm As Form)
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim ctl As Control
Dim userLogin As String
Set db = CurrentDb
Set rst = db.OpenRecordset("SELECT * FROM T_AUDIT")
userLogin = getCurrentUser
Select Case userAction
Case "New"
With rst
.AddNew
![Date] = Now()
![utilisateur] = userLogin
![nomFormulaire] = cForm.Name
![Action] = userAction
![RecordID] = cForm.Controls(RecordID).Value
.Update
End With
Case "Delete"
With rst
.AddNew
![Date] = Now()
![utilisateur] = userLogin
![nomFormulaire] = cForm.Name
![Action] = userAction
![RecordID] = cForm.Controls(RecordID).Value
.Update
End With
Case "Edit"
For Each ctl In cForm.Controls
If (ctl.ControlType = acTextBox) Or (ctl.ControlType = acComboBox) Or (ctl.ControlType = acCheckBox) Then
If (Nz(ctl.Value, "") <> Nz(ctl.OldValue, "")) Then
With rst
.AddNew
![Date] = Now()
![utilisateur] = userLogin
![nomFormulaire] = cForm.Name
![Action] = userAction
![RecordID] = cForm.Controls(RecordID).Value
![champs] = ctl.ControlSource
![ancienneValeur] = ctl.OldValue
![nouvelleValeur] = ctl.Value
.Update
End With
End If
End If
Next ctl
End Select
rst.Close
db.Close
Set rst = Nothing
Set db = Nothing
End Function
这个函数在我要跟踪的表单的beforeUpdate
事件中被调用。
当我尝试编辑绑定的文本框时,会触发错误。
的行If (Nz(ctl.Value, "") <> Nz(ctl.OldValue, "")) Then
是引起错误
表单基于一对多关系链接的2个表。当我从表中编辑字段到关系的"一个"部分时,该函数正在工作,但当我想从"许多"方面编辑字段时,它会抛出错误。
我希望我说的足够清楚,谢谢你
编辑:更多细节
我的表单是基于那个请求的:
SELECT T_REVISION.ID_revision, T_REVISION.fk_ID_proposition, T_REVISION.numero, T_REVISION.fk_etat_revision, T_REVISION.EOTP, T_PROPOSITION.reference_simple, T_PROPOSITION.libelle, T_REVISION.description_localisation
FROM T_PROPOSITION INNER JOIN T_REVISION ON T_PROPOSITION.ID_proposition = T_REVISION.fk_ID_proposition
ORDER BY T_REVISION.numero DESC;
错误从T_PROPOSITION.reference_simple
控制触发。错误3251发生时:我试图编辑T_REVISION。EOTP, t_revision . description_localization 字段。当我编辑T_PROPOSITION时,错误3251没有发生。reference_simple 、 T_PROPOSITION。libelle 。
所以:我能够编辑来自关系的"一个"方面的值,但是当我想编辑"许多"方面似乎我无法访问oldValue属性
如何解决这个问题?
不完全是答案,但是评论区不合适…
如果在If (Nz(ctl.Value, "") <> Nz(ctl.OldValue, "")) Then
之前添加2行:
debug.print ctl.Name, ctl.value
debug.print ctl.name, ctl.oldvalue
这将允许您查看错误是否链接到特定控件和特定属性,并缩小搜索范围。
Edit:在您编辑您的OP后,表明问题出现在您加入的"许多"方面,我认为您应该将表单更改为"主表单-子表单"架构。这将允许您正确跟踪每个TABLE的更新。
当您没有指定弹出错误的行时,很难确切地知道是什么导致了错误,但有一种明显的可能性。
你在控制循环中捕获Null
If (Nz(ctl.Value, "") <> Nz(ctl.OldValue, "")) Then
更重要的是,你缺少一个前括号作为" ("
但是,当将控件的值和旧值分配给记录集时,您不会捕获这种可能性。
![ancienneValeur] = ctl.OldValue
![nouvelleValeur] = ctl.Value
你的字段ancienneValeur
和nouvelleValeur
允许null或零长度字段分配给他们吗?在任何情况下,您都应该保持一致,并确保在将空值分配给表的字段之前捕获空值。
![ancienneValeur] = NZ(ctl.OldValue,"")
![nouvelleValeur] = NZ(ctl.Value,"")