OpenDatabase Method Example (MDB)

The following example returns a Database variable that points to the current database. Then it opens a different database called Another.mdb by using the OpenDatabase method. The procedure then enumerates all TableDef objects in both databases.

To try this example, create a new database called Another.mdb, close it, and place it in the same directory as the database from which you are running the code.

Sub OpenAnother()
    Dim wsp As Workspace
    Dim dbs As Database, dbsAnother As Database
    Dim tdf As TableDef
    
    ' Return reference to current database.
    Set dbs = CurrentDb
    ' Return reference to default workspace.
    Set wsp = DBEngine.Workspaces(0)
    ' Return reference to Another.mdb.
    Set dbsAnother = wsp.OpenDatabase("Another.mdb")
    ' Enumerate all TableDef objects in each database.
    Debug.Print dbs.Name & ":"
    For Each tdf in dbs.TableDefs
        Debug.Print tdf.Name
    Next tdf
    Debug.Print
    Debug.Print dbsAnother.Name & ":"
    For Each tdf in dbsAnother.TableDefs
        Debug.Print tdf.Name
    Next tdf
    Set dbs = Nothing
    Set dbsAnother = Nothing
End Sub