473,396 Members | 1,898 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

A97 syntax for specifying currently open database in DAO...

MLH
Suppose I'm in an open database (northwind.mdb). Is there
a shorter form of doing the following after I've dim'd dbsNorthwind
as database...

Set dbsNorthwind = OpenDatabase("Northwind.mdb")

???

Or, should I always use the above syntax in DAO situations where
I just want to move about a recordset in the database updating
records as I see fit?
Nov 13 '05 #1
11 2075
umm...
currentdb
or
dbengine(0)(0)

Nov 13 '05 #2
MLH
Thanks pietlinden.

umm...
currentdb
or
dbengine(0)(0)


Nov 13 '05 #3
pi********@hotmail.com wrote in
news:11**********************@g49g2000cwa.googlegr oups.com:
umm...
currentdb
or
dbengine(0)(0)


The latter can fail in certain cases (immediately after a wizard has
been run).

CurrentDB().Name will never return anything other than the name of
db that the human user thinks is open in the user interface.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 13 '05 #4
pi********@hotmail.com wrote in
news:11**********************@g49g2000cwa.googlegr oups.com:
umm...
currentdb
or
dbengine(0)(0)


Sorry, forgot one little bit:

CurrentDB() refreshes all collections when called, so it's a heavier
hit, performance-wise, than DBEngine(0)(0). However, that difference
would only become apparent in a loop where you were calling
CurrentDB() multiple times (i.e., hundreds or thousands of times in
a row).

Given that this is not a real-world scenario (during the running of
the loop, CurrentDB() can't possibly change its return value, so
there's no purpose in not calling it once before you enter the loop,
in which case we're talking milliseconds of difference between the
two calls, a difference that only becomes apparent when you are
doing something that is stupid to begin with).

However, all that said, you can reduce even that performance
difference to nothing at all by using a global variable to store a
reference to the currently opened MDB, which you set from
CurrentDB() when you open your application.

I wrap this in a self-healing function, because that survives code
resets. The code for it is appended after my signature.

I use dbLocal in any code where I'd be referring to CurrentDB()
properties, or whenever I'd normally be doing Set db = CurrentDB().

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc

Public Function dbLocal(Optional ysnInitialize As Boolean = True) _
As DAO.Database
' 2003/02/08 DWF added comments to explain it to myself!
' 2005/03/18 DWF changed to use Static variable instead
' uses GoTos instead of If/Then because:
' error of dbCurrent not being Nothing but dbCurrent being closed
' would (3420) would then be jumping back into the middle of an
' If/Then statement
On Error GoTo errHandler
Static dbCurrent As DAO.Database
Dim strTest As String

If Not ysnInitialize Then GoTo closeDB

retryDB:
If dbCurrent Is Nothing Then
Set dbCurrent = CurrentDb()
End If
' now that we know the db variable is not Nothing, test
' if it's Open
strTest = dbCurrent.name

exitRoutine:
Set dbLocal = dbCurrent
Exit Function

closeDB:
If Not (dbCurrent Is Nothing) Then
'dbCurrent.close
Set dbCurrent = Nothing
End If
GoTo exitRoutine

errHandler:
Select Case Err.Number
Case 3420 ' Object invalid or no longer set.
Set dbCurrent = Nothing
If ysnInitialize Then
Resume retryDB
Else
Resume closeDB
End If
Case Else
MsgBox Err.Number & ": " & Err.Description, vbExclamation, _
"Error in mdlWB.dbLocal()"
Resume exitRoutine
End Select
End Function
Nov 13 '05 #5
David W. Fenton wrote:
pi********@hotmail.com wrote in
news:11**********************@g49g2000cwa.googlegr oups.com:

umm...
currentdb
or
dbengine(0)(0)

The latter can fail in certain cases (immediately after a wizard has
been run).


I find it consistent if you run a wizard, close the current database
leaving Access open then open another database.

--
[OO=00=OO]
Nov 13 '05 #6
Trevor Best <no****@besty.org.uk> wrote in
news:42***********************@news.zen.co.uk:
David W. Fenton wrote:
pi********@hotmail.com wrote in
news:11**********************@g49g2000cwa.googlegr oups.com:

umm...
currentdb
or
dbengine(0)(0)


The latter can fail in certain cases (immediately after a wizard
has been run).


I find it consistent if you run a wizard, close the current
database leaving Access open then open another database.


But you're not going to know if your call to DBEngine(0)(0) is going
to come immediately after re-opening the database.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 13 '05 #7
David W. Fenton wrote:
Trevor Best <no****@besty.org.uk> wrote in
news:42***********************@news.zen.co.uk:

David W. Fenton wrote:
pi********@hotmail.com wrote in
news:11**********************@g49g2000cwa.googl egroups.com:

umm...
currentdb
or
dbengine(0)(0)

The latter can fail in certain cases (immediately after a wizard
has been run).


I find it consistent if you run a wizard, close the current
database leaving Access open then open another database.

But you're not going to know if your call to DBEngine(0)(0) is going
to come immediately after re-opening the database.


It used to, it was one of the first lines of code in the database, that
always seemed to be executed immediately after re-opening it.

--
[OO=00=OO]
Nov 13 '05 #8
Trevor Best <no****@besty.org.uk> wrote in
news:42***********************@news.zen.co.uk:
David W. Fenton wrote:
Trevor Best <no****@besty.org.uk> wrote in
news:42***********************@news.zen.co.uk:
David W. Fenton wrote:

pi********@hotmail.com wrote in
news:11**********************@g49g2000cwa.goog legroups.com:

>umm...
>currentdb
>or
>dbengine(0)(0)

The latter can fail in certain cases (immediately after a wizard
has been run).

I find it consistent if you run a wizard, close the current
database leaving Access open then open another database.


But you're not going to know if your call to DBEngine(0)(0) is
going to come immediately after re-opening the database.


It used to, it was one of the first lines of code in the database,
that always seemed to be executed immediately after re-opening it.


Well, that means your caching a reference, which is another
suggestion entirely.

I still think the idea that CurrentDB() is a vastly slower than
DBEngine(0)(0) (while absolutely true) is one of the most misleading
and widely misinterpreted facts about Access programming that I know
of.

IT DOESN'T MATTER THAT IT'S SLOWER.

There is no circumstance where you would need to call CurrentDB()
enough times for the difference to make a difference in a real-world
app.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 13 '05 #9

"David W. Fenton" wrote
I still think the idea that CurrentDB() is
a vastly slower than DBEngine(0)(0)
(while absolutely true) is one of the most
misleading and widely misinterpreted
facts about Access programming that
I know of.

IT DOESN'T MATTER THAT IT'S SLOWER.

There is no circumstance where you
would need to call CurrentDB() enough
times for the difference to make a differ-
ence in a real-world app.


And, the same is true of many questions about "which is better/faster"
regarding VBA code.

In many cases the difference is not enough that you would notice it unless
you did that particular thing alone in a loop executed hundreds of thousands
of times, compared with the alternative in a similar loop.

Larry Linson
Microsoft Access MVP


Nov 13 '05 #10
David W. Fenton wrote:
IT DOESN'T MATTER THAT IT'S SLOWER.


Keep yer hair on. Nobody apart from you mentioned the speed in this
thread AFAICS.

--
[OO=00=OO]
Nov 13 '05 #11
Trevor Best <no****@besty.org.uk> wrote in
news:42***********************@news.zen.co.uk:
David W. Fenton wrote:
IT DOESN'T MATTER THAT IT'S SLOWER.


Keep yer hair on. Nobody apart from you mentioned the speed in
this thread AFAICS.


It's a common subtext in discussions of this nature. Many people are
adamant about avoiding CurrentDB() because of the supposed speed
penalty.

And I think those people are nutcases.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 13 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
29
by: shank | last post by:
1) I'm getting this error: Syntax error (missing operator) in query expression on the below statement. Can I get some advice. 2) I searched ASPFAQ and came up blank. Where can find the "rules"...
19
by: Nicolas Fleury | last post by:
Hi everyone, I would to know what do you think of this PEP. Any comment welcomed (even about English mistakes). PEP: XXX Title: Specialization Syntax Version: $Revision: 1.10 $...
12
by: ColinWard | last post by:
Hi. I am trying to run the following code when the user clicks a button, but I am getting a syntax error in the SQL. I have a feeling it has to do with brackets. Can anyone help? here is the...
6
by: Melkor Ainur | last post by:
Hello, I'm attempting to build an interpreter for a pascal-like language. Currently, I don't generate any assembly. Instead, I just build an abstract syntax tree representing what I've parsed...
7
by: kosta | last post by:
hello! one of my forms communicates with a database, and is supposed to add a row to a table using an Insert statement... however, I get a 'oledb - syntax error' exception... I have double...
3
by: jinhy82 | last post by:
Hi! I am currently creating a Registration form which contained: UserID Password, FirstName and LastName. These details would be inserted into Ms Access when I click submi button. But I...
0
by: unixNAB | last post by:
MySQL 5.0.x Hello, I'm trying to work out but the MySQL reference guide to the grant syntax is a little off, it says... so i wanted to do something like grant all on %_log.* to .......;...
11
by: fniles | last post by:
One of our application uses VB6 and Access97 database. Another application uses VB.NET 2005. This morning for about 15 seconds when the application tries to read either a query or a table from the...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.