Public Function AuditChanges(RecordID As String, UserAction As String)
On Error GoTo auditerr
Dim DB As Database
Dim rst As Recordset
Dim clt As Control
Dim UserLogin As String
Set DB = CurrentDb
Set rst = DB.OpenRecordset("select * from tbl_AuditTrail", adOpenDynamic)
UserLogin = Environ("UserName")
Select Case UserAction
Case "New"
With rst
.AddNew
![DateTime] = Now()
!UserName = UserLogin
!FormName = ScreenActiveForm.Name
!Action = UserAction
!RecordID = Screen.ActiveForm.Controls(RecordID).Value
.Update
End With
Case "Delete"
With rst
.AddNew
![DateTime] = Now()
!UserName = UserLogin
!FormName = ScreenActiveForm.Name
!Action = UserAction
!RecordID = Screen.ActiveForm.Controls(RecordID).Value
.Update
End With
Case "Edit"
For Each clt In Screen.ActiveForm.Controls
If (clt.ControlType = acTextBox _
Or clt.ControlType = acComboBox) Then
If Nz(clt.Value) <> Nz(clt.OldValue) Then
With rst
.AddNew
![DateTime] = Now()
!UserName = UserLogin
!FormName = ScreenActiveForm.Name
!Action = UserAction
!RecordID = Screen.ActiveForm.Controls(RecordID).Value
!FieldName = clt.ControlSource
!OldValue = clt.OldValue
!newvalue = clt.Value
.Update
End With
End If
End If
Next clt
End Select
rst.Close
DB.Close
Set rst = Nothing
Set DB = Nothing
auditerr:
MsgBox Err.LastDllError & " : " & Err.Description, vbCritical, "Error"
Exit Function
End Function
如果你像=AuditChanges()
一样调用你的函数,很明显它不能工作,因为函数期望根据它的定义恰好有2个参数:
Public Function AuditChanges(RecordID As String, UserAction As String)
这就是为什么你会得到错误
参数个数无效。
该函数需要2
参数(RecordID
和UserAction
),而您需要输入0
。
所以你需要提交RecordID
和UserAction
。例如:
=AuditChanges("YourRecordIDControlName", "New")