Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2015
    Location
    Wales. Land of the sheep.
    Posts
    1,228

    Argument not optional.

    I'm using this code to try and create an audit trail on my database.


    Code:
    Private Sub Form_BeforeUpdate(Cancel As Integer)
        If Not Me.NewRecord Then Call AuditChanges("Order_ID")
    End Sub
    This refers to:

    Code:
    Option Compare Database
    Option Explicit
    '
    ' ================================================
    ' Code by Martin Green Email: martin@fontstuff.com
    ' Visit my Office Tips website @ www.fontstuff.com
    ' YouTube tutorials www.youtube.com/martingreenvba
    ' ================================================
    '
    Sub AuditChanges(IDField As String, UserAction As String)
        On Error GoTo AuditChanges_Err
        Dim cnn As ADODB.Connection
        Dim rst As ADODB.Recordset
        Dim ctl As Control
        Dim datTimeCheck As Date
        Dim strUserID As String
        Set cnn = CurrentProject.Connection
        Set rst = New ADODB.Recordset
        rst.Open "SELECT * FROM tblAuditTrail", cnn, adOpenDynamic, adLockOptimistic
        datTimeCheck = Now()
        strUserID = Environ("USERNAME")
        Select Case UserAction
            Case "EDIT"
                For Each ctl In Screen.ActiveForm.Controls
                    If ctl.Tag = "Audit" Then
                        If Nz(ctl.Value) <> Nz(ctl.OldValue) Then
                            With rst
                                .AddNew
                                ![DateTime] = datTimeCheck
                                ![UserName] = strUserID
                                ![FormName] = Screen.ActiveForm.Name
                                ![Action] = UserAction
                                ![RecordID] = Screen.ActiveForm.Controls(IDField).Value
                                ![FieldName] = ctl.ControlSource
                                ![OldValue] = ctl.OldValue
                                ![NewValue] = ctl.Value
                                .Update
                            End With
                        End If
                    End If
                Next ctl
            Case Else
                With rst
                    .AddNew
                    ![DateTime] = datTimeCheck
                    ![UserName] = strUserID
                    ![FormName] = Screen.ActiveForm.Name
                    ![Action] = UserAction
                    ![RecordID] = Screen.ActiveForm.Controls(IDField).Value
                    .Update
                End With
        End Select
    AuditChanges_Exit:
        On Error Resume Next
        rst.Close
        cnn.Close
        Set rst = Nothing
        Set cnn = Nothing
        Exit Sub
    AuditChanges_Err:
        MsgBox Err.Description, vbCritical, "ERROR!"
        Resume AuditChanges_Exit
    End Sub
    But I get the error Argument not optional on the first code. Is it obvious to anyone why that is? This seems to work in the example and I cant see a difference.

  2. #2
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    The Sub calls for two arguments

    Sub AuditChanges(IDField As String, UserAction As String)

    and you're only providing one

    Call AuditChanges("Order_ID")

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  3. #3
    Join Date
    Jun 2015
    Location
    Wales. Land of the sheep.
    Posts
    1,228
    Hi, In the example on the website, its as I have written in parts. But I appreciate you explaining why this didn't work. There are other parts on the example I may use now I know why.

    Understanding the error is more important than fixing it. so thanks again.

    Richmond sausages and Yorkshire tea are the best.

  4. #4
    orange's Avatar
    orange is online now Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    Andy,

    I have recently worked with a poster re Martin Green's Audit routine. There are issues with Deletes in his code.
    The thread and a sample database with revamped Audit processes that works with Forms and Subforms and handles Deletes is located here.
    Last edited by orange; 05-10-2017 at 07:29 PM. Reason: spelling

  5. #5
    Join Date
    Jun 2015
    Location
    Wales. Land of the sheep.
    Posts
    1,228
    This will take me some time to implement I think, but it looks simple enough. That's great, thanks for that.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 5
    Last Post: 02-28-2017, 09:57 PM
  2. Optional argument in function call.
    By Robeen in forum Access
    Replies: 2
    Last Post: 12-06-2016, 04:40 PM
  3. error argument not optional
    By slimjen in forum Programming
    Replies: 10
    Last Post: 03-20-2013, 09:31 PM
  4. Compile Error: Argument not optional
    By tariq1 in forum Programming
    Replies: 4
    Last Post: 08-11-2012, 01:06 PM
  5. Compile Error: Argument Not Optional
    By bg18461 in forum Access
    Replies: 1
    Last Post: 12-01-2010, 08:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums