473,385 Members | 1,772 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,385 software developers and data experts.

When is NOTHING better than nothing?

OK, so I know to:

Dim RS As ADODB.Recordset
Set RS = CurrentProject.Connection.Execute("EXEC " & "mySPROCname")

'make all sorts of cool stuff happen here...

RS.Close
Set RS = Nothing

and I also:

Dim cmd As ADODB.command
Set cmd = Nothing

Dim prm As ADODB.Parameter
Set prm = Nothing

Dim ctl As Control
Set ctl = Nothing

Dim objCbo As Object
Set objCbo = Nothing

I have always assumed these are necessary, but am I missing anything?

Thanks,
lq

Nov 13 '05 #1
17 2104
Lauren Quantrell wrote:
OK, so I know to:

Dim RS As ADODB.Recordset
Set RS = CurrentProject.Connection.Execute("EXEC " & "mySPROCname")

'make all sorts of cool stuff happen here...

RS.Close
Set RS = Nothing

and I also:

Dim cmd As ADODB.command
Set cmd = Nothing

Dim prm As ADODB.Parameter
Set prm = Nothing

Dim ctl As Control
Set ctl = Nothing

Dim objCbo As Object
Set objCbo = Nothing

I have always assumed these are necessary, but am I missing anything?

Thanks,
lq


Its far more important to ensure you close those Recordsets since they are a popular
source of memory leaks. As a rule I "Set x = Nothing" for anything I set but the need for
that is heavily debated here. At most it means a reference is leaked. YMMV.

--
'---------------
'John Mishefske
'---------------
Nov 13 '05 #2
But...

Dim str as String
Set str = Nothing

Dim x as Integer
Set x = Nothing

???

Nov 13 '05 #3
Lauren Quantrell wrote:
But...

Dim str as String
Set str = Nothing

Dim x as Integer
Set x = Nothing

???


No..no.. You don't need to use the Set with these data types. Objects use the Set command
to allocate memory and then create a reference to that memory space. The basic data types
do not require this. Instead you'd use:

Dim str as String
str = Nothing

Dim x as Integer
x = Nothing

But objects (like Recordsets or Forms) need to be Set. Check the on-line help for more info.

--
'---------------
'John Mishefske
'---------------
Nov 13 '05 #4
I expect that I have created more ADODB objects than most, perhaps any,
here having used them by choice since 1999.
I do not close ADODB objects. I do not set them to nothing. My work is
calculation intensive. In it, the creation of ADODB recordsets and
their use can be repeated many many times.
I have never experienced a problem from my failure to do so; none of my
clients has reported a problem that can be traced to such a cause.

Nov 13 '05 #5
John Mishefske wrote:
Lauren Quantrell wrote:
But...

Dim str as String
Set str = Nothing

Dim x as Integer
Set x = Nothing

???


No..no.. You don't need to use the Set with these data types. Objects
use the Set command to allocate memory and then create a reference to
that memory space. The basic data types do not require this. Instead
you'd use:

Dim str as String
str = Nothing

Dim x as Integer
x = Nothing

But objects (like Recordsets or Forms) need to be Set. Check the on-line
help for more info.


I should have looked at this closer. You only assign Nothing to an Object. A better
example would be:

Dim str as String
str = "some text"

Dim x as Integer
x = 6

--
'---------------
'John Mishefske
'---------------
Nov 13 '05 #6
Using the close method frees system resources such as data and locks;
setting the object to nothing releases the memory associated with the
object.

Having had experience of failure to close a COM object causing an
application to stay in memory I always close and set to nothing (as
appropriate) any objects I create.

It's entirely up to you whose advice you follow but I can't see the problem
in an extra couple of code lines per object created, as opposed to spending
half a day trying to track down an obscure bug caused by failure to release
a COM object properly.
--
Terry Kreft

"Lauren Quantrell" <la*************@hotmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
OK, so I know to:

Dim RS As ADODB.Recordset
Set RS = CurrentProject.Connection.Execute("EXEC " & "mySPROCname")

'make all sorts of cool stuff happen here...

RS.Close
Set RS = Nothing

and I also:

Dim cmd As ADODB.command
Set cmd = Nothing

Dim prm As ADODB.Parameter
Set prm = Nothing

Dim ctl As Control
Set ctl = Nothing

Dim objCbo As Object
Set objCbo = Nothing

I have always assumed these are necessary, but am I missing anything?

Thanks,
lq

Nov 13 '05 #7
MLH
<snip>
It's entirely up to you whose advice you follow but I can't see the problem
in an extra couple of code lines per object created, as opposed to spending
half a day trying to track down an obscure bug caused by failure to release
a COM object properly.


On the subject of COM objects, I know nothing (and NOTHING).
Therefore,

Dim MyKnowledge As ADODB.Recordset
Set MyKnowledge = CurrentProject.Connection.Execute("EXEC " &
"mySPROCname")

- AND -

Set MyKnowledge = Nothing

Are identical for me. Would like to know more, starting with "What are
COM objects? -and- How do I make them in A97?" Would appreciate
a point in the right direction. Thx.
Nov 13 '05 #8

"lylefair" <ly***********@aim.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
I expect that I have created more ADODB objects than most, perhaps any,
here having used them by choice since 1999.
I do not close ADODB objects. I do not set them to nothing. My work is
calculation intensive. In it, the creation of ADODB recordsets and
their use can be repeated many many times.
I have never experienced a problem from my failure to do so; none of my
clients has reported a problem that can be traced to such a cause.


Same here. I have been using ADODB objects extensively for nearly as long.
Ever since A2K hit the streets. I've gotten away from setting them to
nothing and have been unable to detect any sort of problem. The one
precaution that I still employ is to always close recordset objects. Some
of the users leave their app logged in all day, creating thousands of
recordsets. No problems.

Nov 13 '05 #9
"Lauren Quantrell" <la*************@hotmail.com> wrote in
news:11**********************@f14g2000cwb.googlegr oups.com:
But...

Dim str as String
Set str = Nothing

Dim x as Integer
Set x = Nothing


These are not objects.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 13 '05 #10
John Mishefske <jm**********@SPAMyahoo.com> wrote in
news:pM****************@tornado.rdc-kc.rr.com:
Lauren Quantrell wrote:
But...

Dim str as String
Set str = Nothing

Dim x as Integer
Set x = Nothing


No..no.. You don't need to use the Set with these data types.
Objects use the Set command to allocate memory and then create a
reference to that memory space. The basic data types do not
require this. Instead you'd use:

Dim str as String
str = Nothing

Dim x as Integer
x = Nothing

But objects (like Recordsets or Forms) need to be Set. Check the
on-line help for more info.


Nothing is used for items that you use SET to initialize. THat means
objects.

If you try to use "= Nothing" with non-objects, you get a compile
error.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 13 '05 #11
"Lauren Quantrell" <la*************@hotmail.com> wrote in
news:11**********************@g14g2000cwa.googlegr oups.com:
OK, so I know to:

Dim RS As ADODB.Recordset
Set RS = CurrentProject.Connection.Execute("EXEC " &
"mySPROCname")

'make all sorts of cool stuff happen here...

RS.Close
Set RS = Nothing

and I also:

Dim cmd As ADODB.command
Set cmd = Nothing

Dim prm As ADODB.Parameter
Set prm = Nothing

Dim ctl As Control
Set ctl = Nothing

Dim objCbo As Object
Set objCbo = Nothing

I have always assumed these are necessary, but am I missing
anything?


Lyle says that it's not necessary with ADO, because the memory leaks
between VBA and ADO don't exist as they do between DAO and VBA.

I don't use ADO, so I can't confirm or deny his assertion.

If I *did* use ADO, I think I'd do manual cleanup, anyway, just in
case.

Now, some of those things are neither ADO nor DAO, such as Control
and Object. Object, I"d definitely set to Nothing, since it can be
any number of things and is very likely to be entirely external to
Access (as opposed to DAO and ADO objects, which, while external to
Access are known by Access; i.e., Access is designed to work well
with them).

Control is different, as it's entirely Access. But Michael Kaplan
once advised in this newsgroup that it's potentially possible that
looping through the controls collection could leave an implicit
reference to a control unreleased, so I always do this:

Dim ctl As Control

For Each ctl In Me.Controls
[do whatever]
Next ctl
Set ctl = Nothing

So, whenever I walk a collection using a specific variable type
(tdf, qdf, ctl, etc.) I set it to nothing, because each iteration of
the loop is going to do an implicit SET = operation.

The exception is when I use a generic variable type:

Dim varItem as Variant

For Each varItem In Me!lstMyList.ItemsSelected
[do whatever]
Next varItem

In that case, the variant data type is one that is never SET to a
value, so there is no need for setting it to Nothing, since it can't
hold an implicit object reference.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 13 '05 #12
David W. Fenton wrote:
Nothing is used for items that you use SET to initialize. THat means
objects.

If you try to use "= Nothing" with non-objects, you get a compile
error.


It was a cut and paste error. I immediately followed up that post with a correction.

--
'---------------
'John Mishefske
'---------------
Nov 13 '05 #13
David W. Fenton wrote:
"Lauren Quantrell" <la*************@hotmail.com> wrote in
news:11**********************@f14g2000cwb.googlegr oups.com:

But...

Dim str as String
Set str = Nothing

Dim x as Integer
Set x = Nothing

These are not objects.

It was a cut and paste error. I immediately followed up that post with a correction.

--
'---------------
'John Mishefske
'---------------
Nov 13 '05 #14
The best book I have read to explain COM is "Programming Distributed
Applications With Com & Microsoft Visual Basic 6.0 (Programming/Visual
Basic)", there is a later book which deals with COM+ but the explanation of
the basics is a bit more sketchy.

You can pick it up from Amazon ...
http://www.amazon.com/gp/product/157...books&v=glance

at a ridiculous $1.50.

--
Terry Kreft

"MLH" <CR**@NorthState.net> wrote in message
news:0u********************************@4ax.com...
<snip>
It's entirely up to you whose advice you follow but I can't see the
problem
in an extra couple of code lines per object created, as opposed to
spending
half a day trying to track down an obscure bug caused by failure to
release
a COM object properly.


On the subject of COM objects, I know nothing (and NOTHING).
Therefore,

Dim MyKnowledge As ADODB.Recordset
Set MyKnowledge = CurrentProject.Connection.Execute("EXEC " &
"mySPROCname")

- AND -

Set MyKnowledge = Nothing

Are identical for me. Would like to know more, starting with "What are
COM objects? -and- How do I make them in A97?" Would appreciate
a point in the right direction. Thx.

Nov 13 '05 #15
John Mishefske <jm**********@SPAMyahoo.com> wrote in
news:fk****************@tornado.rdc-kc.rr.com:
David W. Fenton wrote:
Nothing is used for items that you use SET to initialize. THat
means objects.

If you try to use "= Nothing" with non-objects, you get a compile
error.


It was a cut and paste error. I immediately followed up that post
with a correction.


Maybe so, but it raised a question that I thought was worth
answering, anyway.

Who's to say that someone encountering your post in the Google
Groups archive will see the correction?

I didn't mean my post as any criticism of you, certainly.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 13 '05 #16
David W. Fenton wrote:
John Mishefske <jm**********@SPAMyahoo.com> wrote in
news:fk****************@tornado.rdc-kc.rr.com:

David W. Fenton wrote:
Nothing is used for items that you use SET to initialize. THat
means objects.

If you try to use "= Nothing" with non-objects, you get a compile
error.


It was a cut and paste error. I immediately followed up that post
with a correction.

Maybe so, but it raised a question that I thought was worth
answering, anyway.

Who's to say that someone encountering your post in the Google
Groups archive will see the correction?

I didn't mean my post as any criticism of you, certainly.


I agree. I should have taken a minute to review my post before sending.

--
'---------------
'John Mishefske
'---------------
Nov 13 '05 #17
MLH
A buck-n-a-half! Now that's decent.
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
On Thu, 20 Oct 2005 10:06:48 +0100, "Terry Kreft"
<te*********@mps.co.uk> wrote:
The best book I have read to explain COM is "Programming Distributed
Applications With Com & Microsoft Visual Basic 6.0 (Programming/Visual
Basic)", there is a later book which deals with COM+ but the explanation of
the basics is a bit more sketchy.

You can pick it up from Amazon ...
http://www.amazon.com/gp/product/157...books&v=glance

at a ridiculous $1.50.


Nov 13 '05 #18

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

Similar topics

13
by: Seth Spearman | last post by:
Hey guys, I have the following code: '****************************************************** If Not Me.NewRecord Then Dim rs As DAO.Recordset Dim strBookmark As String Set rs =...
5
by: Jerry Hull | last post by:
I'm working with a database developed by an untrained person over several years - and on a network that has recently been upgraded with a new server installed and MS office upgraded from 2K (I...
4
by: anonymous | last post by:
Thanks your reply. The article I read is from www.hakin9.org/en/attachments/stackoverflow_en.pdf. And you're right. I don't know it very clearly. And that's why I want to understand it; for it's...
12
by: M.L. | last post by:
When loaded from my hard drive, the webpage I'm working on renders fine on IE6, Firefox 2.0.0.1, and Opera 9.02. However, after uploading it to the web there are 2 images that fail to display on...
2
by: dave | last post by:
Hi, I have searched for the answer for this error message without success. I have seen the question many times though:) I create an ASP.NET project (VS 2005, C#), and use a very simple .mdf...
44
by: Smokey Grindle | last post by:
I have a list box on my form, but I need to databind it to a data table that is a private member of the form's class... so I basically have Public Class MyForm priate m_MyTable as new datatable...
6
by: Homer J. Simpson | last post by:
Hi all, I have enough experience with HTML/classic ASP to get by, and I'm trying to learn ASP.NET. Traditionally, I've taken the habit of breaking out extra-long CSS files into multiple,...
14
by: Martin Wells | last post by:
When I have errors in a program, whether they be compiler errors, linker errors, or if the program crashes when running, I have a list of things I check for initially. If I get an error for...
10
by: Andy B | last post by:
Is it safe to make a method that returns a match collection or nothing? or is it better to just return a match collection and have the code outside the method validate that match collection is...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...

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.