Search code examples
vbams-access

MS Access VBA Module error " Invalid Argument" on this line?


I am trying to create an audit log for an access database, but now the code seems to break down here with Error 3001:

Code:

Set rst = DB.OpenRecordset("SELECT * from tbl_AuditLog", adOpenDynamic)

Here's the full module code:

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

I have stepped into the function and found the line I highlighted as being the problem.

The information comes from a form, and is in the beforeupdate via this code:

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

Now, I am completely new to VBA, so if anyone can tell me what I borked up, or link me to a guide, I would be very grateful.


Solution

  • adOpenDynamic is not a valid argument for the DAO OpenRecordset method.