CurrentDb <> DBEngine(0)(0) (1 Viewer)

Guus2005

AWF VIP
Local time
Today, 21:46
Joined
Jun 26, 2007
Messages
2,599
CurrentDb should be equivalent to DBEngine.Workspaces(0).Databases(0) or DBEngine(0)(0) for short.

I always use CurrentDb because it is easier to type. In the code below Currentdb creates an error while DBEngine(0)(0) doesn't. Why is that?
When is one prefered above the other?
Code:
Public Sub EchoFieldnames()

    Dim tdf As DAO.TableDef
    Dim fld As DAO.Field
    
'    Set tdf = CurrentDb.TableDefs("tblCRB_IMPORT") 'Creates error:3420: Object invalid or no longer set.
    Set tdf = DBEngine(0)(0).TableDefs("tblCRB_IMPORT")
    
    For Each fld In tdf.Fields ‘<<<<where the error occures
        Debug.Print fld.name & ":";
    Next fld
    
    Debug.Print

End Sub
I use this code to get the fieldnames behind eachother with a : separator.
 
Last edited:

Guus2005

AWF VIP
Local time
Today, 21:46
Joined
Jun 26, 2007
Messages
2,599
Next time i'll google it first...

A collegue of mine pointed me to this usenet posting from Michael Kaplan, Mar 16, 2003.

It's an old posting but still can be true to this day.
 

Galaxiom

Super Moderator
Staff member
Local time
Tomorrow, 05:46
Joined
Jan 20, 2009
Messages
12,268
Can't do this:
Code:
Set tdf = CurrentDb.TableDefs("tblCRB_IMPORT")

But surprisingly you can do this which looks the same:
Code:
Dim db As DAO.Database
Dim tdf As DAO.TableDef
 
    Set db = CurrentDb
    Set tdf = db.TableDefs("tblCRB_IMPORT")

But it does give some insight to what is going on.

CurrentDb is not an Object itself but a Method of the Application object.
Code:
Application.CurrentDb()

Essentially it is a function that calls up a pointer to the DBEngine(0)(0) object. When you set the db object variable it gets the value of the function.

Used directly in the Set command that function doesn't understand the argument it is given.
 

Users who are viewing this thread

Top Bottom