usage of nothing

  • Thread starter Thread starter Pierre
  • Start date Start date
P

Pierre

Hi all,

when i have finish with a recordset,

i use rst.close to close it before exiting the function.

I can see often that people will do something like

set rst = nothing.

Is it better, what is the difference?

Do i have to close the recordset before using rst = nothing

Regards

pierre
 
Rule 1. If you opened it, close it
=======================
If you used OpenRecordset(), the use the Close method.
If you did not OpenRecordset(), e.g.:
Set rs = Me.RecordsetClone
then do not close it.

Rationale: In a perfect world, Access would detect when the object you
opened is going out of scope, and automatically close it. There have been
bugs in some versions of Access where this did not happen. As a result, it
was not possible to close Access (except by taking a sledgehammer to it
through the Windows Task Manager.)

There are also cases where closing something you did NOT open causes errors.
For an example, see:
http://allenbrowne.com/ser-37.html

Rule 2. Set your object variables to Nothing
===============================
This is good programming practice, but not as important as Rule 1 in Access.
You can see that closing an object is not the same as setting to nothing,
e.g.:
rs.Close
Debug.Print (rs Is Nothing)
will generate False.

This rule applies not only at the end of a procedure, but also if you plan
to reuse the object, e.g.:
rs.Close
Set rs = Nothing
Set rs = db.OpenRecordset(...
 
Back
Top