473,386 Members | 1,817 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Do I need to use Nothing after each rst.close?

My program looks like this:

Dim db As DAO.Database, rst As DAO.Recordset
Set db = CurrentDb

Set rst = db.OpenRecordset("SELECT * FROM tUnpaidInvoiceIDs WHERE TxnType =
'Invoice'")
Do some work
rst.close

Set rst = db.OpenRecordset("SELECT * FROM tUnpaidInvoiceIDs WHERE TxnType =
'Credit Memo'")
Do some work
rst.close

this pattern is repeated several more times and then

rst.close
Set rst = Nothing
Set db = Nothing

Do I need to set rst to Nothing after each rst.close or only after the final
close?
Nov 12 '05 #1
11 5965
On Thu, 15 Jan 2004 17:33:48 GMT, "Karl Irvin" <88********@comcast.net> wrote:
My program looks like this:

Dim db As DAO.Database, rst As DAO.Recordset
Set db = CurrentDb

Set rst = db.OpenRecordset("SELECT * FROM tUnpaidInvoiceIDs WHERE TxnType =
'Invoice'")
Do some work
rst.close

Set rst = db.OpenRecordset("SELECT * FROM tUnpaidInvoiceIDs WHERE TxnType =
'Credit Memo'")
Do some work
rst.close

this pattern is repeated several more times and then

rst.close
Set rst = Nothing
Set db = Nothing

Do I need to set rst to Nothing after each rst.close or only after the final
close?


What you are doing is correct, and it's what most people do. Personally,
though, I try to be more rigorous and set everything to Nothing (of the kinds
of things that need to be explicitly set to Nothing) at the same time I close
it. Since you can put statements on the same line using a colon separator, I
usually say...

rst.Close: Set rst = Nothing
Nov 12 '05 #2

One more question, if you don't mind me butting in...

Do you need rst.Close or can you simply to straight to Set rst = Nothing???

Thanks

Chris
"Steve Jorgensen" <no****@nospam.nospam> wrote in message
news:bh********************************@4ax.com...
On Thu, 15 Jan 2004 17:33:48 GMT, "Karl Irvin" <88********@comcast.net> wrote:
My program looks like this:

Dim db As DAO.Database, rst As DAO.Recordset
Set db = CurrentDb

Set rst = db.OpenRecordset("SELECT * FROM tUnpaidInvoiceIDs WHERE TxnType ='Invoice'")
Do some work
rst.close

Set rst = db.OpenRecordset("SELECT * FROM tUnpaidInvoiceIDs WHERE TxnType ='Credit Memo'")
Do some work
rst.close

this pattern is repeated several more times and then

rst.close
Set rst = Nothing
Set db = Nothing

Do I need to set rst to Nothing after each rst.close or only after the finalclose?

What you are doing is correct, and it's what most people do. Personally,
though, I try to be more rigorous and set everything to Nothing (of the

kinds of things that need to be explicitly set to Nothing) at the same time I close it. Since you can put statements on the same line using a colon separator, I usually say...

rst.Close: Set rst = Nothing

Nov 12 '05 #3
ya******@yahoo.com (Yamafopa!) wrote in
<bu**********@news.storm.ca>:
Do you need rst.Close or can you simply to straight to Set rst =
Nothing???


Yes, you need to do both, because they are too different things:

1. Set rst.Close releases the memory structures the recordset was
stored in.

2. Set rst = Nothing clears the pointer to that memory structure,
but doesn't necessarily release it.

The recordset variable is a pointer to a memory structure, so
clearing the pointer is a separate operation from destroying that
memory structure.

And, obviously, you've got to do the .Close before clearing the
pointer.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 12 '05 #4


David

Thank you for the concise answer. I suspected it was something like that
but had not been able to find it in a reference.

chris
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 12 '05 #5
Steve Jorgensen <no****@nospam.nospam> wrote in message news:<bh********************************@4ax.com>. ..
On Thu, 15 Jan 2004 17:33:48 GMT, "Karl Irvin" <88********@comcast.net> wrote:
My program looks like this:

Dim db As DAO.Database, rst As DAO.Recordset
Set db = CurrentDb

Set rst = db.OpenRecordset("SELECT * FROM tUnpaidInvoiceIDs WHERE TxnType =
'Invoice'")
Do some work
rst.close

Set rst = db.OpenRecordset("SELECT * FROM tUnpaidInvoiceIDs WHERE TxnType =
'Credit Memo'")
Do some work
rst.close

this pattern is repeated several more times and then

rst.close
Set rst = Nothing
Set db = Nothing

Do I need to set rst to Nothing after each rst.close or only after the final
close?


What you are doing is correct, and it's what most people do. Personally,
though, I try to be more rigorous and set everything to Nothing (of the kinds
of things that need to be explicitly set to Nothing) at the same time I close
it. Since you can put statements on the same line using a colon separator, I
usually say...

rst.Close: Set rst = Nothing


Incidentally can you see anything why this wouldn't work?

Function GetRidOf(ParamArray RecordsetsToClose() )
On Error Resume Next
Dim y
For Each y In RecordsetsToClose
y.Close
Set y = Nothing
Next
End Function

Usage:
GetRidOf rstA,rstB,rstC
Nov 12 '05 #6
On 20 Jan 2004 03:35:05 -0800, dr**********@hotmail.com (Terry Bell) wrote:
Steve Jorgensen <no****@nospam.nospam> wrote in message news:<bh********************************@4ax.com>. ..
On Thu, 15 Jan 2004 17:33:48 GMT, "Karl Irvin" <88********@comcast.net> wrote:
>My program looks like this:
>
>Dim db As DAO.Database, rst As DAO.Recordset
>Set db = CurrentDb
>
>Set rst = db.OpenRecordset("SELECT * FROM tUnpaidInvoiceIDs WHERE TxnType =
>'Invoice'")
>Do some work
>rst.close
>
>Set rst = db.OpenRecordset("SELECT * FROM tUnpaidInvoiceIDs WHERE TxnType =
>'Credit Memo'")
>Do some work
>rst.close
>
>this pattern is repeated several more times and then
>
>rst.close
>Set rst = Nothing
>Set db = Nothing
>
>Do I need to set rst to Nothing after each rst.close or only after the final
>close?
>


What you are doing is correct, and it's what most people do. Personally,
though, I try to be more rigorous and set everything to Nothing (of the kinds
of things that need to be explicitly set to Nothing) at the same time I close
it. Since you can put statements on the same line using a colon separator, I
usually say...

rst.Close: Set rst = Nothing


Incidentally can you see anything why this wouldn't work?

Function GetRidOf(ParamArray RecordsetsToClose() )
On Error Resume Next
Dim y
For Each y In RecordsetsToClose
y.Close
Set y = Nothing
Next
End Function

Usage:
GetRidOf rstA,rstB,rstC


Actually, I don't think that works because "y" is a copy of the reference, so
the original reference will not be cleared. I did a test of something similar
that does work though...

Public Sub GetRidOf(ParamArray RecordsetsToClose() )
Dim lngIndex as Long
On Error Resume Next
For lngIndex = LBound(RecordsetsToClose) To UBound(RecordsetsToClose)
RecordsetsToClose(lngIndex).Close
Set RecordsetsToClose(lngIndex) = Nothing
Next
End Sub

Nov 12 '05 #7
dr**********@hotmail.com (Terry Bell) wrote in
news:92*************************@posting.google.co m:
Steve Jorgensen <no****@nospam.nospam> wrote in message
news:<bh********************************@4ax.com>. ..
On Thu, 15 Jan 2004 17:33:48 GMT, "Karl Irvin" <88********@comcast.net>
wrote:
>My program looks like this:
>
>Dim db As DAO.Database, rst As DAO.Recordset
>Set db = CurrentDb
>
>Set rst = db.OpenRecordset("SELECT * FROM tUnpaidInvoiceIDs WHERE
>TxnType = 'Invoice'")
>Do some work
>rst.close
>
>Set rst = db.OpenRecordset("SELECT * FROM tUnpaidInvoiceIDs WHERE
>TxnType = 'Credit Memo'")
>Do some work
>rst.close
>
>this pattern is repeated several more times and then
>
>rst.close
>Set rst = Nothing
>Set db = Nothing
>
>Do I need to set rst to Nothing after each rst.close or only after the
>final close?
>


What you are doing is correct, and it's what most people do.
Personally, though, I try to be more rigorous and set everything to
Nothing (of the kinds of things that need to be explicitly set to
Nothing) at the same time I close it. Since you can put statements on
the same line using a colon separator, I usually say...

rst.Close: Set rst = Nothing


Incidentally can you see anything why this wouldn't work?

Function GetRidOf(ParamArray RecordsetsToClose() )
On Error Resume Next
Dim y
For Each y In RecordsetsToClose
y.Close
Set y = Nothing
Next
End Function

Usage:
GetRidOf rstA,rstB,rstC


The funtion closes the recordsets, but it does not release the object
pointers. Try:

Sub temp()
Dim r As DAO.Recordset
Dim s As String
s = "Object Pointer"
Set r = DBEngine(0)(0).OpenRecordset("SELECT * FROM MSysObjects")
s = s & vbNewLine & "Before GetRidOf:" & ObjPtr(r)
GetRidOf r
s = s & vbNewLine & "After GetRidOf:" & ObjPtr(r)
Set r = Nothing
s = s & vbNewLine & "After SetToNothing:" & ObjPtr(r)
MsgBox s
End Sub

Function GetRidOf(ParamArray RecordsetsToClose())
On Error Resume Next
Dim y
For Each y In RecordsetsToClose
y.Close
Set y = Nothing
Next
End Function
--
Lyle
(for e-mail refer to http://ffdba.com/contacts.htm)
Nov 12 '05 #8
Steve Jorgensen <no****@nospam.nospam> wrote in message news:<tl********************************@4ax.com>. ..
On 20 Jan 2004 03:35:05 -0800, dr**********@hotmail.com (Terry Bell) wrote:
Steve Jorgensen <no****@nospam.nospam> wrote in message news:<bh********************************@4ax.com>. ..
On Thu, 15 Jan 2004 17:33:48 GMT, "Karl Irvin" <88********@comcast.net> wrote:

>My program looks like this:
>
>Dim db As DAO.Database, rst As DAO.Recordset
>Set db = CurrentDb
>
>Set rst = db.OpenRecordset("SELECT * FROM tUnpaidInvoiceIDs WHERE TxnType =
>'Invoice'")
>Do some work
>rst.close
>
>Set rst = db.OpenRecordset("SELECT * FROM tUnpaidInvoiceIDs WHERE TxnType =
>'Credit Memo'")
>Do some work
>rst.close
>
>this pattern is repeated several more times and then
>
>rst.close
>Set rst = Nothing
>Set db = Nothing
>
>Do I need to set rst to Nothing after each rst.close or only after the final
>close?
>

What you are doing is correct, and it's what most people do. Personally,
though, I try to be more rigorous and set everything to Nothing (of the kinds
of things that need to be explicitly set to Nothing) at the same time I close
it. Since you can put statements on the same line using a colon separator, I
usually say...

rst.Close: Set rst = Nothing


Incidentally can you see anything why this wouldn't work?

Function GetRidOf(ParamArray RecordsetsToClose() )
On Error Resume Next
Dim y
For Each y In RecordsetsToClose
y.Close
Set y = Nothing
Next
End Function

Usage:
GetRidOf rstA,rstB,rstC


Actually, I don't think that works because "y" is a copy of the reference, so
the original reference will not be cleared. I did a test of something similar
that does work though...

Public Sub GetRidOf(ParamArray RecordsetsToClose() )
Dim lngIndex as Long
On Error Resume Next
For lngIndex = LBound(RecordsetsToClose) To UBound(RecordsetsToClose)
RecordsetsToClose(lngIndex).Close
Set RecordsetsToClose(lngIndex) = Nothing
Next
End Sub

Well that's interesting.
So is this what happens?
When you Dim y and do the For Each, on each iteration
it assigns to y a pointer to the recordset structure.
The Close works, because that's an operation on the
structure that's being pointed to.
But Set y = Nothing ... does nothing because y is not
explicitly associated with an object variable?
And thanks Lyle I tried your test with Steve's code
and confirmed it worked.
Terry
Nov 12 '05 #9
On 20 Jan 2004 18:42:27 -0800, dr**********@hotmail.com (Terry Bell) wrote:
Steve Jorgensen <no****@nospam.nospam> wrote in message news:<tl********************************@4ax.com>. ..
On 20 Jan 2004 03:35:05 -0800, dr**********@hotmail.com (Terry Bell) wrote:

Well that's interesting.
So is this what happens?
When you Dim y and do the For Each, on each iteration
it assigns to y a pointer to the recordset structure.
The Close works, because that's an operation on the
structure that's being pointed to.
But Set y = Nothing ... does nothing because y is not
explicitly associated with an object variable?
And thanks Lyle I tried your test with Steve's code
and confirmed it worked.
Terry


Close - very close, but not quite.

This following may benefit some future Google searchers...

OK, the recordset itself is an object instance located somewhere in memory as
you suggest. Technically, it is stored as a structure (with its own
references to other structures), but we don't think of them as mere
structures, because metaphorically, they have behaviors as well as data,
though technically, the behaviors are implemented by the inderlyng class code.

Anyway, the code that calls our function has a recordset variable which is
just little more than a pointer to the recordset instance. The little more
part is that the recordset also knows what references are pointing to it, and
will go out of scope only when all of those pointers go out of scope or are no
longer pointing to that object.

So, when you call the function, the parameters are passed by reference unless
you say otherwise, which means that when you manipulate the reference, you are
directly manipulating the variable passed to the reference by the calling
code. It turns out this works with paramarrays as well such that each element
in the array is actually a reference to the variable that was passed to it,
not a copy of its contents. In a way, you could say the parameter in the
function operates as a pointer to the Recordset variable in the calling
function, which in turn acts as a pointer to the Recordset instance somewhere
in memory. Note that there is actually only one thing pointing directly to
the Recordset instance, so the reference counter is 1.

Now, if I make a new Recordset variable (or variant that will hold a recordset
reference), and I copy the parameter to it, I now have 2 direct references to
the recordset, and on indirect reference. The reference count is 2. If I set
the second variable to Nothing, it reduces the reference count to 1 because
the second one no longer points to the recordset, but it has no effect on the
original reference which what accounts for the 1-count.

When you run the For Each loop, the item variable is being successively
assigned copies of the Recordset references just as if you manually assigned
them, so each Recordset's reference count goes briefly to 2, then back down to
1. The calling code's references all still point to the instance, so it does
not get released.

When we loop through, and directly assign the paramarray elements to Nothing,
these assignments apply to the variables passed to those parameters directly,
so the original reference is being released, and the Recordset can get
released.
Nov 12 '05 #10
Sub temp()
Dim r As DAO.Recordset
Dim s As String
s = "Object Pointer"
Set r = DBEngine(0)(0).OpenRecordset("SELECT * FROM MSysObjects")
s = s & vbNewLine & "Before GetRidOf:" & ObjPtr(r)
GetRidOf r
s = s & vbNewLine & "After GetRidOf:" & ObjPtr(r)
Set r = Nothing
s = s & vbNewLine & "After SetToNothing:" & ObjPtr(r)
MsgBox s
End Sub

Sub GetRidOf(ByRef RecordsetsToClose As DAO.Recordset)
On Error GoTo errHandler

RecordsetsToClose.Close
Set RecordsetsToClose = Nothing

Exit Sub
errHandler:
Debug.Print Err.Number, Err.Description, Err.Source
End Sub
Nov 12 '05 #11
mi****@execpc.com (almish) wrote in news:95d47401.0401202151.73ef0347
@posting.google.com:
Sub temp()
Dim r As DAO.Recordset
Dim s As String
s = "Object Pointer"
Set r = DBEngine(0)(0).OpenRecordset("SELECT * FROM MSysObjects")
s = s & vbNewLine & "Before GetRidOf:" & ObjPtr(r)
GetRidOf r
s = s & vbNewLine & "After GetRidOf:" & ObjPtr(r)
Set r = Nothing
s = s & vbNewLine & "After SetToNothing:" & ObjPtr(r)
MsgBox s
End Sub

Sub GetRidOf(ByRef RecordsetsToClose As DAO.Recordset)
On Error GoTo errHandler

RecordsetsToClose.Close
Set RecordsetsToClose = Nothing

Exit Sub
errHandler:
Debug.Print Err.Number, Err.Description, Err.Source
End Sub


I believe the original poster wished to pass an array of recordsets.

--
Lyle
(for e-mail refer to http://ffdba.com/contacts.htm)
Nov 12 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: jason | last post by:
Access 2000: I have a customer-inventory table I need to loop through and compile a list of all the inventory items the customer is tracking. The problem I am finding is that a simple loop...
2
by: Bobby | last post by:
Hello everyone I have a question. The school I am working for is in the beginning process of having a webpage that will direct students to download there homework and be able to view there info...
3
by: NewmanBT | last post by:
As you can see from the code below, several textboxes will be dynamically created and each will be tied to an org. The ChangeComment function should allow for an update to the database whenever...
106
by: xtra | last post by:
Hi Folk I have about 1000 procedures in my project. Many, many of them are along the lines of function myfuntion () as boolean on error goto er '- Dim Dbs as dao.database Dim Rst as...
2
by: Keith Kowalski | last post by:
I anm opening up a text file reading the lines of the file that refer to a tif image in that file, If the tif image does not exist I need it to send an email stating that the file doesn't exist...
6
by: kenshiro | last post by:
Hi All, I'm having a problem with some VBA code in one of my Access 2003 databases. I'm getting the following error when running code behind a command button on a form: "Item not found in this...
4
by: Paul H | last post by:
A typical chunk of code...... Set db = CurrentDb Set rs = db.OpenRecordset("tblFoo") <Do some stuff here> 'How much of the stuff below do I need? 'Do I need to close the recordset?...
9
by: pic078 via AccessMonster.com | last post by:
I need serious help - I have a frontend/backend Access database (2 MDE Files) that remains stuck in task manager after exiting the application - you can't reopen database after exiting as a result...
1
by: lxxslay3rxxl | last post by:
Let's say I haf 3 project that I wan to list, each project should have 1 value assign to it, but if i remove 1 of the value for 1 of the project, and i tried to list all 3 projects, I get this error:...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.