Just to clarify for you. When you do not specify the return type of a function in VB6, the type is
Variant.
You should always specify the return type.
Objects are handled by the OS in a way that keeps track of how many references there are to it.
When you do this:
Code:
Dim rstTarget As ADODB.Recordset
Set rstTarget = New ADODB.Recordset
the reference count for the object in memory that rstTarget refers to = 1. If you did this:
Code:
Dim rstTarget As ADODB.Recordset
Code:
Dim rstTwo As ADODB.Recordset
Set rstTarget = New ADODB.Recordset
Set rstTwo = rstTarget
that object in memory has 2 references to it. If you set rstTarget to Nothing,
the recordset would still exist. If you then set rstTwo to Nothing, the OS would
then destroy the object in memory. Objects are destroyed when their reference
count = 0.
So. You can do this to return a recordset:
Code:
Public Function XdX() As ADODB.RecordSet
Set XdX = New ADODB.RecordSet
XdX.Open . . .
End Function
and you would return the recordset that was created with the New statement, and opened
with the .Open method. Even if you didn't open the recordset, you could still pass the unopened recordset created with the New statement this way.
Some people find that a little too terse for comfort. So you can do this instead:
Code:
Public Function XdX() As ADODB.RecordSet
Dim r As ADODB.RecordSet
Set r = New ADODB.RecordSet
r.Open . . .
Set XdX = r
Set r = Nothing ' This is highly recommended. it makes it clear that
' youâre through with the reference, and avoids relying
' on âautomaticâ behavior. Itâs possible that future
' versions of VB will end implicit release of object
' variables. Not likely but itâs really better practice.
End Function