Ok...I thought I had tested this all out. I even THOUGHT about what you suggested before I posted and in hindsight, I know what I did wrong, and it was PROFOUNDLY dumb (just as I expected).
Anyway, based on your suggestion, this is what I came up with:
'the original function code, without the debugging stuff
Public Function GetRS(NameOfTable As String) As ADODB.Recordset
On Error GoTo HandleError
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
rs.Open NameOfTable, CurrentProject.Connection,
Set GetRS = rs
'rs.Close
Set rs = Nothing
ExitHere:
Exit Function
HandleError:
MsgBox Err.Number & ": " & Err.Description
Resume ExitHere
End Function
--------------------------
'a new sub to accept the recordset and process it
Public Sub ReadRS(SomeRS As ADODB.Recordset)
Do
Debug.Print SomeRS(0)
SomeRS.MoveNext
Loop Until SomeRS.EOF
End Sub
So if I call my sub in the immediate window like so:
ReadRS GetRS("Product")
I get the list of values from the first column of the table--exactly what is expected. The error I made earlier was to call the sub like this:
ReadRS(GetRS("Product"))
which for reasons that I might be able to explain with a great deal of reflection produces a Type Mismatch error (but at this instant, it's not at all clear to me).
So you've solved the problem. I still have one lingering concern: Why CAN'T I close the recordset in GetRS after I SET the function return, but I can SET it to Nothing? You're correct, it absolutely will not work if the recordset is closed explicitly. Any ideas? (You'll get the points, but I'm interested in a more general conceptual discussion now). Thanks again.
Main Topics
Browse All Topics





by: GreymanMSCPosted on 2004-12-09 at 18:16:51ID: 12789382
Since the error is triggering on exiting the function, the occurance is not actually in the function, but in the call line where the result of GetRS is being used that's producing the error.
You will have something like: Set R = GetRS("ValidTableName")
Make sure that R is actually declared as ADODB.Recordset, because if it's just declared as a Recordset, it may actually be constructed as a DAO.Recordset.
(PS: definitely do not use the 'rs.Close' line inside the function!)