Re: Question from Access 2007 VBA by Hennig et al

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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]
.



Relevant Pages

  • Re: Setting the Startup Options for an Access 2007 database using
    ... Dim dbs As Object ... Set dbs = CurrentDb ... Just to serve as a reminder, this code was taken from an MSDN page for Access ...
    (microsoft.public.access.modulesdaovba)
  • Re: BE database
    ... in your back-end, CurrentDb will still work. ... Dim dbs As DAO.Database ... > set dbs = Currentdb ... Can someone tell me how to declare and set the db to reference my ie. C:\Mydocuments\db1.mdb > ...
    (microsoft.public.access.gettingstarted)
  • Re: tabellen erstellen -> tabelle nicht da
    ... >> den Refresh selbst nachschicken. ... >> des fehlenden Refreshs um so viel schneller. ... > damit die Database Instanz, die er anschliessend für das Recordset ... also eine /neue/ Instanz von CurrentDB ... ...
    (microsoft.public.de.access)
  • Re: Question from Access 2007 VBA by Hennig et al
    ... It'd be similar to the age-old debate between CurrentDb() and DBEnginewhich give you the same references but with different effects. ... I suspect Henning et al recommends this primarily so you have a most current reference without need to refreshing it every time you call if you called CurrentDbin every and each procedure where you need. ... Dim Dbs As DAO.Database ... Set Dbs = CurrentDb ...
    (microsoft.public.access.modulesdaovba)
  • Re: tabellen erstellen -> tabelle nicht da
    ... >> Nach dem Anlegen der neuen Tabelle muss ein Refresh auf die Container ... der bei CurrentDB automatisch gemacht wird. ... > innerhalb der Transaktion noch abgeschlossen ist. ... eine Instanz von CurrentDB gebildet wird, gibt's den Refresh. ...
    (microsoft.public.de.access)