david@epsomdotcomdotau said:
Use of CurrentDB [in that way] is an acceptable coding practice.
Here are some of the statements I was thinking of:
===
MK: "In some cases, the use of CurrentDb on a single line will cause a
non-persistent - object to be created, so you will not be able to use
[that object] if you do not cache it".
===
Andy Barron: "my rule of thumb is don't use Currentdb for anything that
you can't do [completely] in one line."
MK: "This is not CurrentDb's implementation that is responsible, it is
VBA.... they are the ones who are charged with the "hidden" obj ref in
this case and they are the ones who free it since they think they are
done." [ IOW, there are cases where creating a new object based off
a currentdb call, will fail. ]
===
Andy Barron: "The rule of thumb that I follow is to use currentdb by
itself (rather than setting a variable to currentdb) only if what I'm
doing can be done in one line. For multi-line tasks, I always use a db
variable. There are some multi-line cases, like opening recordsets,
where currentdb by itself will work, but mostly it won't."
MK: "Basically, as soon as that one line is done running, the db
variable itself will be gone. So unless the thing you get from db can
stand on its own and keep an internal ref when needed (such as
recordsets do) then the other variable won't work. The best two
examples are the Recordset, which DOES hold such a ref... and the
TableDef, which does not."
MK: "A Querydef ref actually loads the query and does not require the
db ref to be there still (provided the db is open)..... while the
TableDef ref does not "open the table" and it needs to call back to the
db ref to get info."
===
And I guess the definitive post:
MK:
<quote>
Well, you took it a step further than I intended. CurrentDB is a
special case because each call to it creates a new object, so that the
TableDef can try to go back to its parent and fail as it is looking at
a pointer that is basically not valid. So it is the tdf that needs the
db and can't get it anymore. So the following two operations are
equivalent:
Set tdf = CurrentDb.TableDefs(0)
? tdf.Name
Set db = CurrentDb
Set tdf = db.TableDefs(0)
set db = Nothing
? tdf.Name
In both cases the attemot to use tdf will fail. Basically because DAO
does not understand enough to know where to go back to. AFAIK, any time
the latter case works, the former case will work, however. That is the
basic reason for a difference between CurrentDb and DBEngine(0)(0) in
this "one line" scenario.... an implementation detail in Access.
Note that
set db = DBEngine(0)(0)
set tdf = db.TableDefs(0)
set db = Nothing
? tdf.Name
will succeed, as will
Set tdf = DBEngine(0)(0).TableDefs(0)
? tdf.Name
because it is guaranteed that the TableDef has a "place" to go back to
in the case where you use DBEngine (at least in Access -- in VB you can
see the same kind of issue in cases where you can invalidate the (0)(0)
database.
</quote>
===
I can't say I 've re-tested all this in Access 2000 & later. I just
remember MK's previous warnings, & comply with those
Cheers,
TC