views:

1531

answers:

4

In Access, what is the difference between these two statements?

DBEngine.BeginTrans

and

DBEngine.Workspaces(0).BeginTrans

The documentation for both leads to the same place.

+4  A: 

Have a look here: DAO Workspace
And then here: DAO Workspace: Opening a Separate Transaction Space

(The links are for MFC, but they're applicable to whatever you're coding in.)

DBEngine.Workspaces(0) is the default workspace. Other workspaces can be created, which let you work with separate sessions; the idea is that BeginTrans and EndTrans apply to the whole workspace, but if you need to do stuff outside that transaction, you can create another workspace and use it independently of your transactions in the first workspace.

Personally, I never had occasion to use more than one workspace when doing DAO in VBA. * shrug *

Kyralessa
Thanks for the answer and the links, K. I was actually trying to figure out the difference between DBEngine.BeginTrans and DBEngine.Workspaces(0).BeginTrans.
Shane
Well, the point of the above is that there is no difference. Workspaces(0) is the default workspace, so DBEngine.BeginTrans is shorthand for DBEngine.Workspaces(0).BeginTrans. Unless you're using multiple workspaces, you may as well use the shorter form.
Kyralessa
There *is* a difference, but correct that in the *default workspace* it works out to the same thing.
John Mo
No, John Mo, there is NO difference. DBEngine.BeginTrans and DBEngine.Workspaces(0).BeginTrans are the same thing. If you want to call BeginTrans on a different workspace, you have to specify it.
Kyralessa
A: 

My own answer:

It appears that DBEngine.BeginTrans and DBEngine.Workspaces(0).BeginTrans do the same thing because this code works (see below). "Workspaces" is the default member of DBEngine.

Dim db As Database
Set db = CurrentDb

DBEngine.BeginTrans
db.Execute "Update Table1 SET CITY='Newark'"
DBEngine.Workspaces(0).Rollback
Shane
That's the way it "appears." There's more to it.
John Mo
A: 

In the Access application interface, you can only have one database container open at a time. In VBA code, you can open multiple database instances within a Workspace. See the help file documentation for the Workspace.OpenDatabase method (or http://msdn.microsoft.com/en-us/library/bb243164.aspx) for an example where more than one Database is opened in one Workspace.

One would infer that when transactions are supported by all of the underlying Databases that are open in a Workspace, the BeginTrans method of the Workspace would apply across all of the databases. I suspect there be dragons there, but I'm sure it would work with two MDBs inside of one Workspace. When there's only one database open in the Workspace, Workspace.BeginTrans and Database.BeginTrans are indeed the same.

John Mo
Hey, John. Thanks for weighing in. My question was about the difference between "DBEngine.Workspaces(0).BeginTrans" vs "DBEngine.BeginTrans". BeginTrans is not a method of the Database object in DAO. It's a method of the DBEngine and Workspace objects, and I was trying to understand the distinction.
Shane
Sorry I overlooked the DBEngine bit. Apparently I've begun to outlive my usefulness in Access. The DAO Database object *used to have* a BeginTrans method. Sorry for the diversion into the arcane.
John Mo
A: 

As Spock once said, a difference which makes no difference is no difference...

TonBill