What's Benefit of Dim db vs Currentdb ?

  • Thread starter Thread starter Mike Thomas
  • Start date Start date
M

Mike Thomas

I'm repeatedly typing something like this:

strSQL = "Select * from..."
set rst = currentdb.openrecordset ( _
strSQL, dbopenforwardonly)

Most postings I see here suggest:

Dim db as database
strSQL = "Select * from..."
set rst = db.openrecordset ( _
strSQL, dbopenforwardonly)

What is the advantage of instantiating a db database object?

Many thanks
Mike Thomas
 
Mike said:
I'm repeatedly typing something like this:

strSQL = "Select * from..."
set rst = currentdb.openrecordset ( _
strSQL, dbopenforwardonly)

Most postings I see here suggest:

Dim db as database
strSQL = "Select * from..."
set rst = db.openrecordset ( _
strSQL, dbopenforwardonly)

What is the advantage of instantiating a db database object?


CurrentDb is a FUNCTION that constructs and returns a sort
of clone of the current database. Each time you call the
function you get a different instance of a database object.
If you want to use the same instance of the database object
returned by CurrentDb(), you need to set an object variable.
Think about the implications of this statement:
(CurrentDb Is CuurentDb) is never true

CurrentDb() does a lot of work to refresh all the database's
collections. While this guarantees a reliable result, it
does take a lot of work. CurrentDb() is somewhere around
5000 times slower than DbEngine(0).Databases(0), so you do
not want to use CurrentDb() inside a loop or any more often
than needed.

You **may** be able to use DbEngine(0).Databases(0) to refer
to the actual current database object, but there is no
guarantee that Dabases(0) is the right reference. Besides,
the collections in DbEngine may not be refreshed to the
level that CurrentDb() takes care of.

One other point, some operations set property values in the
db object, which will be immediately discarded if you don't
set the object variable. An example of this is the
RecordsAffected property that's set by the Execute method.

Oh yes, if you do do it the "right" way and Set db =
CurrentDb(), don't forget to dereference the object when
you're done with it by using Set db = Nothing
 
http://support.microsoft.com/default.aspx?scid=kb;en-us;167173

This article is for Access 95 & 97 but the behaviour is unchanged in
subsequent versions (at least 2000, XP/2002), AFAIK (and if it *IS* changed,
I support multiple versions so its immaterial).

Set rst = CurrentDB.OpenRecordset...
-CurrentDB is a temp pointer that loses it's value as soon as the rst is
set, leaving the rst without a db reference in any subsequent usage, causing
compile errors.

Set db = CurrentDB
Set rst = db.OpenRecordset
-1) db is declared as a database object and gets an assignment
- 2) the non-temp database object is used to qualify the rst object. This
reference is maintained.

I think I've seen some people indicate that it is no longer necessary to do
this. However, the few times I've tried to do it without explicitly
declaring a db variable I get errors, so I ignore "it isn't necessary"
suggestions.
 
Back
Top