AmendConstitution_ArnoldForPresident said:
I don't see any downside either! This is a keeper. If that changes,
I'll report back in this thread. This was also instructive to me.
Besides inspiring me to peruse Syscmd details, I found that
Report_rptFoo was a valid object. That was a big surprise - and that
it could be "New"-ed. Your help is all useful.
Me too, at first blush, but both you and Help on New use the word
"reference," so killing the reference doesn't kill the source. If you
set a reference to a printer and Nothing-ed it, the H.P. wouldn't die.
Apparently "New" is just like that with the addition of causing
creation. Or at least, that's what I'm adopting as my meager-minded
interpretation
Well, that's not exactly right. As a rule, when the last reference to
an object is destroyed, whether because you explicitly set it to Nothing
or because the reference variable goes out of scope or is reset, the
object itself is destroyed. When you use this same technique with forms
(e.g., Set frm = New Form_frmFoo), as is described in the help file, you
*must* keep a reference to the form instance for as long as you want the
form to remain open, because when the last reference to that form object
goes out of scope, the form will be closed and the instance will be
destroyed. The customary way around this is to add the instance to a
global collection of your own, from which you remove it when the form is
closed.
So why is it different with reports? Here's the key: when you open a
*form* in a normal way, via the user interface or the OpenForm method or
action, a reference to that form is added to the application's Forms
collection. It's this reference that keeps the form "alive". This is
called the "default instance" of the form. But when you open a
non-default instance of the form by using "Set object = New ...", a
reference to this instance is *not* added to the Forms collection. So
it's up to you to keep the object alive by maintaining a reference to
it.
I expected reports to work the same way. But my tests show that when
you use this technique to create an instance of the report, a reference
to the new instance *is* added to the Application's Reports collection,
just as it is when you use OpenReport or open the report via the user
interface. And since there's a reference to the report in the Reports
collection, the report remains open even after your local reference is
destroyed. All instances of the same report have the same name, so you
can't index the Reports collection by name to find a specific instance
among several, but if that doesn't bother you, you appear to be home
free. In my tests, I set the Caption property of each instance to a
different value so that it would be easy to find the one I want, whether
programmatically or while looking at the report preview windows.
Side musing...is "Nothing"-ing just an inexpensive defensive move now?
In A97+ or A00+ are {Set...=Nothing}'s still meaningful, or just
evidence of good form and stylishness by "Coders' coders?" :=)
Interesting you should ask. Some of my colleagues and I have been
discussing just this question. Theoretically, VB should destroy all
local variables when they go out of scope, so you shouldn't have to set
anything but a global or static object variable to Nothing. However,
there appears to have been a bug, at some time in the past and *maybe*
still in recent versions, when Access didn't always release and destroy
local object references as they went out of scope. This was reported as
happening with DAO objects like Recordset and Database objects. The
result was that Access wouldn't close, but would remain open (if
invisible) until you used Task Manager to blow it away.
I don't know the specifics of this bug, and I'm not entirely convinced
that it wasn't always a result of using static or global objects rather
than local ones. But if there *was* a bug, it would have affected
Access 97, and I don't know if it's fixed now -- in current SPs of A97
or in later versions of Access -- or not. So I still recommend the
practice of closing what you open and explicitly setting all object
variables to Nothing when you're done with them.