1

The image shows the actual error.I am using this following code for creating an audit trail in MS-access using vba script, but getting 3001 : Invalid argument error. I have already checked the table names field names etc. But couldn't solve the issue. Can someone please help?

I tried looking up the 3001 : invalid argument, but I can not determine exactly what is causing the problem in my scenario.

I would really appreciate it if anyone can help me out with this.

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 audit", 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.Number & " : " & Err.Description, vbCritical, "Error"
Exit Function

End Function  

Thank you, Rito

  • 1
    In which line does the error appear? That would be helpful. Set a breackpoint and debug the code with F8 and look where he jumps to `auditerr`. But I think you have `!FromName = ScreenActiveForm.Name` replace it with `!FormName = Screen.ActiveForm.Name`. Or even better would be `!FormName = Me.Name`. This mistake appear more often in your code. – Strawberryshrub Sep 07 '18 at 04:44
  • I see that I'm facing the following error. – Ritaban Sengupta Sep 07 '18 at 17:53
  • Perfect... yes this worked.. thank you so much!! – Ritaban Sengupta Sep 07 '18 at 21:03

1 Answers1

0

Try this

Set rst = DB.OpenRecordset("select * from audit", dbOpenDynaset)

Santosh
  • 12,175
  • 4
  • 41
  • 72