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.
Function AddCustomer(Rec As DAO.Recordset) As Long
< statements to populate the recordset >
.Bookmark = .LastModified
AddCustomer = ![CustomerId] 'Return the index value
Sub AddOrder(Rec As DAO.Recordset, Id As Long)
![CopyOfCustomerId] = Id
< ...etc... >
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?
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.
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:
Dim TempCustId, TempItemId as Long
when it should have read:
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.