Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2004
    Location
    U.S.
    Posts
    12

    Unanswered: Compile Error: ByRef arg type mismatch

    I'm getting a compile error that I don't understand: "ByRef argument type mismatch." Below are some code excerpts, translated as "customers and orders" to protect the innocent

    This is in the module of a data input form (gathers order and customer info) where I use a function to add a row to the customers table, which returns the index value of the new record (CustomerId as Long).

    This value is then passed to a sub that adds a row to the orders table, including a copy of the CustomerId passed as an argument.
    Code:
    Function AddCustomer(Rec As DAO.Recordset) As Long
       With Rec
          .AddNew
          < statements to populate the recordset > 	
          .Update 
          .Bookmark = .LastModified
          AddCustomer = ![CustomerId]   'Return the index value
          .Close
        End With
    End Function
    
    Sub AddOrder(Rec As DAO.Recordset, Id As Long)
       With Rec
          .AddNew
          ![CopyOfCustomerId] = Id
         < ...etc... >
    End Sub
    
    Sub MyOuterProcedure
        Dim Rs As DAO.Recordset
        Dim TempCustId as Long
        Set Rs = dbMyapp.OpenRecordset("tblCustomers")
        TempCustId = AddCustomer(Rs)
        Set Rs = dbMyapp.OpenRecordset("tblOrders")
    -->AddOrder Rs, TempCustID
     Compile Error: "ByRef argument type mismatch"
    The error occurs in the next to last line, show by the arrow. If I delete the line it compiles & runs fine to that point.

    Is there an error in this code, or should I be looking elsewhere for the bug?
    Last edited by AJS; 03-05-04 at 04:04.

  2. #2
    Join Date
    Nov 2003
    Posts
    1,487
    Just for the heck of it try puting the DoEvents statement directly after the TempCustId = AddCustomer(Rs).

    See it that works...

  3. #3
    Join Date
    Feb 2004
    Location
    U.S.
    Posts
    12
    Still doesn't compile.

  4. #4
    Join Date
    Feb 2004
    Location
    U.S.
    Posts
    12
    I forgot to mention, in the original question, that TempCustomerId is the highlighted argument when the compiler stops on the "ByRef argument type mismatch" error.

  5. #5
    Join Date
    Nov 2003
    Posts
    1,487
    Well...there you go..I suppose VBA will not allow you to pass a RecordSet as a parameter. Set and Open your RecordSets directly within your Function and Sub procedures. It make things a little clearer anyways.



  6. #6
    Join Date
    Feb 2004
    Location
    U.S.
    Posts
    12
    Follow-up: I found the bug, and I'm embarrassed to say it was a trivial error in a Dim statement. I had written something like:
    Code:
    Dim TempCustId, TempItemId as Long
    when it should have read:
    Code:
    Dim TempCustId as Long, TempItemId as Long
    I think this comes from having used more Pascal than Basic. I didn't find the error until now because it has correct syntax, but I guess TempCustId was just getting the default type (Variant?) instead of the intended Long integer. Hence the "type mismatch."

    Meanwhile, after CyberLynx' 2nd post, but before I found the bug, I rewrote the code with recordsets local to their procedures. So the question of whether recordsets can be passed as arguments remains open.

    --Andy
    Last edited by AJS; 03-09-04 at 09:50.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •