when can I use CurrentDb

F

Fia

Hi
I wonder when I can use CurrentDb or when I have to make an object of type
Database.
I tried to use this code

Dim fld as Field
Dim tblDef as TableDef
Set tblDef = CurrentdB.TableDefs("Categories")
With tblDef
For Each fld In .Fields
Debug.Print " " & fld.Name & " " & fld.Type
Next
end With

,but I got the error 3420 "object invalid or no longer set"
Then I used an object of type Database and it worked.

Are there any prescriptions on when you can use CurrentDb and when you have
to make an object of type Database?

Thank's
Fia
 
A

Allen Browne

Every time you use CurrentDb, it creates another object.
You can get away with opening recordsets that hold that object open while
they are open, but you cannot get away with referring to the DAO objects
themselves.

Well, you can get away with it if you use dbEngine(0)(0) instead of
CurrentDb, but that is not recommended when you are altering the database
schema (adding/deleting/modifying tables/fields/properties), because
dbEngine(0)(0) is not always kept up to date, whereas Currentdb is.

So, declare a Database variable and set it to CurrentDb() when you are
modifying the data structure, or need to refer to the database object
repeatedly, or wish to get at the structure in the database.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top