Whats the difference between rs.close vs rs = nothing in a RecordSet
Asked Answered
W

4

21

I often find it confusing as to when it is appropriate to use:

rs.Close 

opposed to

Set rs = Nothing

I can understand needing to close a connection to a source, but should I be using both when the variable falls out of scope?

Can I just set the variable to Nothing in order to skip the step of Closing the connection? Would this be considered a bad practice?

Winstead answered 30/3, 2010 at 2:12 Comment(1)
If you use a With OpenRecordset ... End With pattern then the object is temporary. No need to manually close it or set it to nothing anymore. This allows the scope to be very clearly defined.Dorris
H
16

By using the "Close" method you are closing the connection to the database but is still in the memory where you can open again using the "Open" method.

Setting the recordset to "Nothing" on the other hand releases the object completely from the memory.

Heinz answered 30/3, 2010 at 2:19 Comment(3)
So, does doing one bypass the need for doing the other?Winstead
Not really although you can bypass setting the recordset to nothing and will not encounter any error. Its just that it is the best practice to set the recorset to nothing after you closed it especially when you have no use to that recordset or you will not going to access the same recordset again.Heinz
Your answer refers to database connections, but the question used a recordset. Database variables are different from others in that what you can safely do with them depends on how they were initialized (CurrentDB vs. DBEngine(0)(0)). With a recordset variable, closing the recordset does not close the database connection at all.Firecracker
F
13

The Close method tears down the memory structure.

Setting the variable to Nothing clears the pointer to that memory structure.

Theoretically, clearing the pointer should release the memory the pointer was referring to, because VBA uses reference counting for determining when it can release memory. Unfortunately, various things can go wrong and the reference count can end up out of whack, and memory won't be released even when it should be.

Thus, to be sure you're not subject to memory leaks, or the weird kinds of bugs caused by implicit and unreleased references, you both Close and set to Nothing.

Firecracker answered 2/4, 2010 at 17:17 Comment(0)
S
9

You can set Recordset to Nothing without needing to call Close, according to official documentation:

An alternative to the Close method is to set the value of an object variable to Nothing (Set dbsTemp = Nothing).

More info: Recordset.Close Method (DAO)

Seller answered 9/7, 2015 at 14:59 Comment(1)
Nice, I had been doing it this way for years and had assumed it was best practice. As I was giving a recommendation to someone else to setting to Nothing over using Close I looked it up again to double check which is why I came across this discussion. Appreciate the link to the actual documentation!Stochmal
H
1

Well, in my own experience, if the recorset object(hereinafter referred to as "RS") is declared locally(within the function/procedure, hereinafter referred to as "B") and will not be delivered out to where B is called(hereinafter referred to as "A"), it's safe and suggested to close and set RS to nothing inside B; but in the following situations:

  1. RS is delivered as one of the parmeter argument of B(either byval or byref) from A
  2. RS is declared in B and is to be one of the return value(s) of B for A to use

RS in B should only set to nothing without closing it, orelse the recordset object returned to A(or sent to B as one of the parameter(s) from A) will also be closed and set nothing, making it inaccessible in A, even if you returned RS to A in advance and then close in B!

Hemingway answered 4/3, 2019 at 7:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.