views:

85

answers:

1

Tested on Access 2003 Pro (build 11.8321.8324) SP3.

Steps to reproduce:

  • create a new database.
  • create a new form.
  • put a button on the form.
  • paste the following code in the button's Click event procedure:

Debug.Print Workspaces.Count
Debug.Print CurrentDb.Name

  • close the code editor and form, saving changes.
  • do not skip this step: close Access.
  • re-open Access and your database.
  • open the form
  • click the button
  • click the toolbar button to switch the form to design mode.

You should see the following error dialog: Access error dialog

Does anyone know what is going on here?

A: 

The simple workaround is to call CurrentDb prior to calling Workspaces for the first time:

Debug.Print CurrentDb.Name
Debug.Print Workspaces.Count
Debug.Print CurrentDb.Name

I'll take a shot at demystifying what's going on, but this is just my theory.

The relevant bits of the Access help file are as follows (for CurrentDb):

Note In previous versions of Microsoft Access, you may have used the syntax DBEngine.Workspaces(0).Databases(0) or DBEngine(0)(0) to return a pointer to the current database. In Microsoft Access 2000, you should use the CurrentDb method instead. 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. Microsoft Access still supports the DBEngine(0)(0) syntax, but you should consider making this modification to your code in order to avoid possible conflicts in a multiuser database.

And for the Workspaces collection:

When you first refer to or use a Workspace object, you automatically create the default workspace, DBEngine.Workspaces(0).

It would seem that by creating the default workspace prior to the first call to CurrentDb, you are somehow causing CurrentDb to forget how it should work. It seems that instead of creating a new instance of the current database it just uses the one that's already lying around in the default Workspace.

Of course, this is all conjecture and I'm as curious as you to know the "real answer".

mwolfe02
I actually found that workaround but it didn't get me any closer to understanding what is going on. (nor did reading the Help!) Another workaround I think is to never ever refer to the `Workspaces` collection.
Hugh Allen
That doesn't make any sense. If you have a database open in the user interface, the workspace is already created.
David-W-Fenton
I agree it makes no sense. But it is the best (though admittedly weak) explanation I could come up with for why switching the order of the statements fixes the problem.
mwolfe02
My point is that the explanation, that doing it a different way creates the workspace and doing it the other way does not is simply nonsensical. The cause clearly has to be something else, as the Jet workspace exists when you have a database open. Likely it's the library being used to return the workspace that has the initialization issues.
David-W-Fenton