Must close recordset?

M

Mercy

Hi,
I was just wondering if you have to close a recordset when
you're done with it? And if you do ... why?

Thanks :)
Mercy
 
A

Allen Browne

If you open anything, close it.
If you did not open it, do not close it.
Regardless of whether you open it or not, set all object variables to
Nothing.

Example 1: Opening a recordset:
Dim rs As DAO.Recordset
Set rs = dbEngine(0)(0).OpenRecordset("MyTable")
'do something
rs.Close 'Correct: you opened it.
Set rs = Nothing 'Set any object to Nothing

Example 2: Refereing to an already open recordset.
Dim rs As DAO.Recordset
Set rs = Forms("MyForm").RecordsetClone
'do something
rs.Close '<=WRONG! It was already open.
Set rs = Nothing 'Set any object to Nothing
 
A

Allen Browne

In a perfect world, the software would automatically destroy the objects
when the procedure closes.

Access is actually very good at doing that, but it is not perfect. We are
aware of at least two cases where it can fail to clean up by itself. The
result is that when you try to close Access it minimizes to the task bar,
and cannot be closed without using the Task Manager (Ctrl+Alt+Del) to kill
it.

It is always good practice to clean up. Close what you open. Set all object
variables to nothing.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top