Reputation: 309
I am using Access 2010. How do I get the information in a recordset from one function moved into a different function? The following code is giving me an object variable not set error:
Function Main()
Dim rs as DAO.RecordSet
Dim i as integer
Set rs = QueryResults(i)
Do While Not rs.EOF
debug.print rs!result
rs.MoveNext
Loop
End Function
Function QueryResults(i as Integer) as DAO.RecordSet
Dim stQuery as String
Dim rsResults as DAO.RecordSet
Dim i2 as Integer
i2 = i
stQuery = "Select query that uses i2 to get results"
Set rsResults = CurrentDb.OpenRecordset(stQuery)
End Function
I can step through and see that the QueryResults function is working. I can print out each line from rsResults and see that it has what I want. The problem comes when the code hits the "do while not rs.EOF" line. It says that rs was never set to anything.
What am I doing wrong? Both functions are in the same module. It seems like it shouldn't be that hard to get results from one function into another function.
Upvotes: 1
Views: 91
Reputation: 97131
When you want a function to return something, you must assign that something to the function name.
Change the last part of your QueryResults
function to this:
'Set rsResults = CurrentDb.OpenRecordset(stQuery)
Set QueryResults = CurrentDb.OpenRecordset(stQuery)
End Function
Upvotes: 4