问题描述
我正在尝试为访问数据库创建审核日志,但现在代码似乎在此处崩溃并出现错误 3001:
代码:
Set rst = DB.OpenRecordset("SELECT * from tbl_AuditLog",adOpenDynamic)
这是完整的模块代码:
Option Compare Database
Public Function AuditLog(RecordID As String,UserAction As String)
On Error GoTo AuditErr
Dim DB As Database
Dim rst As Recordset
Dim ctl As Control
Dim UserLogin As String
Set DB = CurrentDb
Set rst = DB.OpenRecordset("SELECT * from tbl_AuditLog",adOpenDynamic)
UserLogin = Environ("Username")
Select Case UserAction
Case "New"
With rst
.AddNew
![EditDate] = Now()
![User] = UserLogin
!FormName = Screen.ActiveForm.Name
!Action = UserAction
!RecordID = Screen.ActiveForm.Controls(RecordID)
.Update
End With
Case "Delete"
With rst
.AddNew
![EditDate] = Now()
![User] = UserLogin
!FormName = Screen.ActiveForm.Name
!Action = UserAction
!RecordID = Screen.ActiveForm.Controls(RecordID)
.Update
End With
Case "Edit"
For Each ctl In Screen.ActiveForm.Controls
If (ctl.controltpe = acTextBox _
Or ctl.ControlType = acComboBox) Then
If Nz(ctl.Value) <> Nz(ctl.OldValue) Then
With rst
.AddNew
![EditDate] = Now()
![User] = UserLogin
!FormName = Screen.ActiveForm.Name
!Action = UserAction
!RecordID = Screen.ActiveForm.Controls(RecordID)
!FieldName = ctl.ControlSource
!OldValue = ctl.OldValue
!NewValue = ctl.Value
.Update
End With
End If
End If
Next ctl
End Select
rst.Close
DB.Close
Set rst = nothing
Set DB = nothing
AuditErr:
MsgBox Err.Number & " : " & " Unable to create audit log " & Err.Description
Exit Function
End Function
信息来自表单,并通过此代码在beforeupdate中:
Private Sub Form_BeforeUpdate(Cancel As Integer)
If Me.NewRecord Then
Call AuditLog("Employee ID","New")
Else
Call AuditLog("Employee ID","Edit")
End If
End Sub
现在,我对 VBA 完全陌生,所以如果有人能告诉我我厌烦了什么,或将我链接到指南,我将不胜感激。
解决方法
adOpenDynamic 不是 DAO OpenRecordset 方法的有效参数。