1

I understand that CurrentDb returns a reference to the currently open database, but where is that set in the first place? I'm working on an Access front end with linked tables to multiple databases, so when/where is the CurrentDb set?

Vanny
  • 259
  • 3
  • 8

1 Answers1

4

CurrentDb is set when a db is opened in the Access session.

If you start Access without opening a db, or close whatever db is open in the current session, CurrentDb is Nothing ...

? CurrentDb Is Nothing
True

But when a database is open, CurrentDb is no longer Nothing.

? CurrentDb Is Nothing
False

CurrentDb is now a DAO Database object ...

? TypeName(CurrentDb)
Database
? CurrentDb.Name
C:\share\Access\database1.mdb
HansUp
  • 95,961
  • 11
  • 77
  • 135
  • 2
    But remember that "The CurrentDb method creates another instance of the current database, while the DBEngine(0)(0) syntax refers to the open copy of the current database. The CurrentDb method enables you to create more than one variable of type Database that refers to the current database" – 4dmonster Oct 15 '14 at 17:25
  • Also remember that `CurrentDb` is not an *object*, it is a *function* that *returns an object*, so using `CurrentDb.SomeMethod` can sometimes lead to [unexpected behaviour later in the code](http://stackoverflow.com/a/21417189/2144390). For more gory details, see another answer [here](http://stackoverflow.com/a/1837922/2144390). – Gord Thompson Oct 15 '14 at 17:47
  • Can you elaborate on "CurrentDb is set when a db is opened in the Access session."? I guess I'm confused because I have an Access front end with a lot of linked tables to different MySQL databases. So who's to say what the current MySQL database actually is? Is the current database just a way of referring to the collection of all linked tables? – Vanny Oct 15 '14 at 19:28
  • 1
    When you open an Access db file in an Access session, `CurrentDb` returns a reference to that db. If that db file contains links to tables or views in another db, `CurrentDb` still returns a reference to the same Access db. It does not change while that db is still open. You can open one of the linked tables in Datasheet View, and `CurrentDb` still returns a reference to the same db --- it does not switch to point to the external db which contains the table which the link points to. You can verify that easily in the Immediate window: `Debug.Print CurrentDb.Name` – HansUp Oct 15 '14 at 21:32
  • Then create a link, check `CurrentDb.Name`. Open the link, view and edit the linked data and check `CurrentDb.Name` again. Remove the link, and `CurrentDb.Name` which still show you the same db. – HansUp Oct 15 '14 at 21:36
  • Yes, currnetdb is a means to refer to the collection of objects, including quires and tables that can well be linked to rather diverse data sources. So your database container can have many objects, each of which link to many different datasources. The currentDB thus lets you reference the current collection of those objects. – Albert D. Kallal Oct 15 '14 at 22:22