Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any important difference between set dbs=currentdb() and using currentDB() directly?

Tags:

ms-access

I have inherited a lot of code that is essentially like this:

dim dbs as dao.database
set dbs = currentdb()
dbs.execute "Some SQL string"
set dbs = nothing

Is there any reason not to recode it as:

currentdb().execute "some  SQL string"

(I know that if I want to use .recordsaffected, currentdb().recordsaffected won't yield usable results).

Are there any benefits from recoding it, other than simplifying the code?

like image 706
ColeValleyGirl Avatar asked Jan 28 '14 21:01

ColeValleyGirl


People also ask

What is CurrentDB in MS Access?

The CurrentDb method provides a way to access the current database from Visual Basic code without having to know the name of the database. After you have a variable that points to the current database, you can also access and manipulate other objects and collections in the DAO hierarchy.

What is Dao database in VBA?

DAO (Data Access Objects) are APIs that give you the ability to write applications that are independent of any particular database management system (DBMS). In other words, DAO is a programming interface that allows you to communicate with data objects in your database using VBA code.

How do I open a recordset in Access VBA?

Opening a Recordset We first need to establish the database we intend to use, in this case it is the currently opened database. We can then use the CurrentDB. OpenRecordSet method to open/create our Recordset.


1 Answers

Simply using CurrentDb.Whatever is a tempting shortcut, but there are quite a few circumstances where it causes strange behaviour. That's because CurrentDb is not an Object itself, it is a Function that returns a copy of the current Database object.

Years ago I swore off trying to use CurrentDb like it was an Object after the umpteenth time I was debugging code that I knew was "right", and it was... once I created a proper DAO.Database object (Set cdb = CurrentDb) and used cdb.Whatever instead of CurrentDb.Whatever.

like image 167
Gord Thompson Avatar answered Sep 20 '22 13:09

Gord Thompson