Re: Question from Access 2007 VBA by Hennig et al
- From: Marshall Barton <marshbarton@xxxxxxxxxx>
- Date: Wed, 06 Jan 2010 19:11:53 -0600
JGPatrick wrote:
Hennig et al is raising more questions for me than it is answering, as you
might guess from my previous two posts.
Hennig et al say the way to establish a reference to the default database is
to create a DAO.database property:
*****
Public dbC as DAO.database
Public Property Get CurrentDbC() As DAO.Database
If (dbC Is Nothing) Then Set dbC = CurrentDb()
Set CurrentDbC = dbC
End Property
*****
and then use the property, for eg:
*****
Sub Prac()
Dim Dbs As DAO.Database
Set Dbs = CurrentDbC
End Sub
******
My question is, why wouldn't you just do the following, which simply uses
the built-in CurrentDb function:
Sub Prac()
Dim Dbs As DAO.Database
Set Dbs = CurrentDb()
End Sub
As Lyle Fairfield has succinctly explained it
"CurrentDb Is CurrentDb is *always* false"
Note that Currentdb is a built-in ***function*** that
constructs a refreshed *copy* of the DbEngine(0)(0) object
each time it is called. [It's not always DbEngine(0)(0) but
that's a different issue]
The reason that the book suggests using your own
function/property to do that is because it only uses
CurrentDb the first time it is called. The reason some
people feel the need to do that is because CurrentDb is
somewhere between 5,000 and 10,000 times slower than just
referring to DbEngine(0)(0) and because you will be using
the same copy all the time so you do not need to declare it
everywhere you use it. But, on modern machines, the time
difference is a very small fraction of a second and you will
not notice it unless you use CurrentDb inside a loop.
The downside is that the copy of CurrentDb may need to be
refreshed if you are not using the Refresh method after
adding items to collections that need to be refreshed. In
my book, forgetting to use Refresh and relying on CurrentDb
to mask the problem is a very poor practice so, IMO, this is
not a valid argument against using what the book said.
--
Marsh
MVP [MS Access]
.
- References:
- Question from Access 2007 VBA by Hennig et al
- From: JGPatrick
- Question from Access 2007 VBA by Hennig et al
- Prev by Date: Re: Nesting methods in Access VBA
- Next by Date: Re: Question from Access 2007 VBA by Hennig et al
- Previous by thread: Re: Question from Access 2007 VBA by Hennig et al
- Next by thread: Re: Question from Access 2007 VBA by Hennig et al
- Index(es):
Relevant Pages
|