Does setting a recordset = nothing accomplish the same as closing
it?
No. When you do this:
Set rs = CurrentDB.OpenRecordset("SQL String")
you have done two things:
1. you've initialized a structure in memory that's populated with
the data from your SQL statement.
2. you've returned a pointer to that memory structure and stored it
in the variable rs.
When you set rs = Nothing, all you're doing is clearing the pointer
-- you're not doing anything to release the memory used by the data
structure it was pointing to. Now, because VBA uses reference
counting to handle memory, if that was the last reference to that
memory structure, VBA should, theoretically, release the memory
(since there's no longer any reference to it). But, of course, if
VBA reference counting worked reliably for releasing memory, life
would be a lot easier. The fact is, that reference counting is an
unreliable method for managing memory, and you end up with memory
leaks.
So, instead of depending on Set rs = Nothing to release the memory
used by the recordset that the pointer stored in the variable rs
refers to, we explicitly close the recordset and then clear the
pointer:
rs.Close
Set rs = Nothing
Both are essential, no matter what MS's code examples my indicate.
I've
read a number of statements that it's smart to do both (close and
set to nothing), but msdn says the following:
"An alternative to the Close method is to set the value of an
object variable to Nothing (Set dbsTemp = Nothing)."
http://msdn.microsoft.com/en-us/library/bb243098.aspx
That sounds like setting to nothing accomplishes the same thing as
closing. Is this correct?
See above. In an ideal world, MSDN would be right. In the real
world, it's not.
Of course, it also depends on what you're doing. Apparently, ADO
doesn't have the memory leaks when used with VBA that DAO does, so
the advice there may work for ADO (and for other libraries, too). We
know for a fact that it does *not* work reliably for DAO in all
versions of Access (that is, it may work reliably in some versions,
but if your code may someday be running in a different version of
Access, you don't want to depend on something specific to the
version in which you wrote it).