Results 1 to 4 of 4
  1. #1
    gaker10 is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2014
    Posts
    262

    Compile Error: ByRef argument type mismatch

    Here's the function:



    Code:
    Private Sub Form_BeforeUpdate(Cancel As Integer)
      Call AuditTrail(Me, ECN#)
    End Sub
    Which calls this code:

    Code:
    Const cDQ As String = """"
    Sub AuditTrail(frm As Form, recordid As Control)
      'Track changes to data.
      'recordid identifies the pk field's corresponding
      'control in frm, in order to id record.
      Dim ctl As Control
      Dim varBefore As Variant
      Dim varAfter As Variant
      Dim strControlName As String
      Dim strSQL As String
      On Error GoTo ErrHandler
      'Get changed values.
      For Each ctl In frm.Controls
        With ctl
        'Avoid labels and other controls with Value property.
        If .ControlType = acTextBox Then
          If .Value <> .OldValue Then
            varBefore = .OldValue
            varAfter = .Value
            strControlName = .Name
            'Build INSERT INTO statement.
            strSQL = "INSERT INTO " _
               & "Audit (EditDate, User, RecordID, SourceTable, " _
               & " SourceField, BeforeValue, AfterValue) " _
               & "VALUES (Now()," _
               & cDQ & Environ("username") & cDQ & ", " _
               & cDQ & recordid.Value & cDQ & ", " _
               & cDQ & frm.RecordSource & cDQ & ", " _
               & cDQ & .Name & cDQ & ", " _
               & cDQ & varBefore & cDQ & ", " _
               & cDQ & varAfter & cDQ & ")"
            'View evaluated statement in Immediate window.
            Debug.Print strSQL
            DoCmd.SetWarnings False
            DoCmd.RunSQL strSQL
            DoCmd.SetWarnings True
          End If
        End If
        End With
      Next
      Set ctl = Nothing
      Exit Sub
    
    
    ErrHandler:
      MsgBox Err.Description & vbNewLine _
       & Err.Number, vbOKOnly, "Error"
    End Sub
    Where Me is for the form, and ECN# is the unique ID.

    It's having an issue with ECN#, which is of type Autonumber. I've tried passing it as Data_ECN# (the table name). I have no idea why I'm getting this error since my data type is fine. Ideas?

  2. #2
    gaker10 is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2014
    Posts
    262
    Ok this is wierd, I replaced ECN# with ECN_ because that is what popped up under the Me. drop down and it seemed to fix the problem. Just how are "_" and "#" related?

  3. #3
    drexasaurus's Avatar
    drexasaurus is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2011
    Location
    Santa Ana, CA
    Posts
    60
    I think the underscore is simply just the default replacement for illegal characters for object names in vba, or at least legal in the designer maybe, but illegal in code. I personally avoid those kind of names in objects or table attributes.

  4. #4
    gaker10 is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2014
    Posts
    262
    I see. Thank you for the explanation.

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

Similar Threads

  1. Replies: 7
    Last Post: 07-24-2013, 02:01 PM
  2. Compile Error: Argument not optional
    By tariq1 in forum Programming
    Replies: 4
    Last Post: 08-11-2012, 01:06 PM
  3. Replies: 7
    Last Post: 11-17-2011, 02:56 PM
  4. byref argument type mismatch error
    By karuppasamy in forum Access
    Replies: 1
    Last Post: 06-22-2011, 09:37 AM
  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