Description
A Database object represents an open database.
Remarks You use the Database object and its methods and properties to manipulate an open database. In any type of database, you can:See Also Database property, OpenDatabase method.
Specifics (Microsoft Access) When working with Data Access Objects from Microsoft Access, you will often need a Database object variable that represents the current database. Use the CurrentDb function to return a Database object for the database that is currently open. This Database object is automatically appended to the Databases collection. For example, suppose you are currently working with the Northwind sample database in Microsoft Access. You can create a Database object that refers to that database by first declaring a Database object variable, then pointing it to the Database object returned by the CurrentDb function.Dim dbs As Database
Set dbs = CurrentDb
You don't need to know the name of the database or its position in the Databases collection in order to use the current database. If you do want to know the name of the current database, check the Name property of the Database object, which contains the path and file name of the database. To find its position in the Databases collection, enumerate through the collection.
You can open only one database at a time in the Microsoft Access window. From Visual Basic code, however, you can create multiple independent Database object variables to represent multiple open databases. In this way, you can manipulate more than one database at a time from code. You can also create multiple Database object variables and point them to the current database.
Note In your Visual Basic code, use the CurrentDb function to return a Database object that refers to the current database, rather than the DBEngine(0)(0) syntax. The CurrentDb function creates another instance of the current database, while the DBEngine(0)(0) syntax refers to the open copy of the current database. Using the CurrentDb function 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 environment.
Example
This example creates a new Database object and opens an existing Database object in the default Workspace object. Then it enumerates the Database collection and the Properties collection of each Database object.
Sub DatabaseObjectX()
Dim wrkJet As Workspace
Dim dbsNorthwind As Database
Dim dbsNew As Database
Dim dbsLoop As Database
Dim prpLoop As Property
Set wrkJet = CreateWorkspace("JetWorkspace", "admin", "", dbUseJet)
' Make sure there isn't already a file with the name of
' the new database.
If Dir("NewDB.mdb") <> "" Then Kill "NewDB.mdb"
' Create a new database with the specified
' collating order.
Set dbsNew = wrkJet.CreateDatabase("NewDB.mdb", _
dbLangGeneral)
Set dbsNorthwind = wrkJet.OpenDatabase("Northwind.mdb")
' Enumerate the Databases collection.
For Each dbsLoop In wrkJet.Databases
With dbsLoop
Debug.Print "Properties of " & .Name
' Enumerate the Properties collection of each
' Database object.
For Each prpLoop In .Properties
If prpLoop <> "" Then Debug.Print " " & _
prpLoop.Name & " = " & prpLoop
Next prpLoop
End With
Next dbsLoop
dbsNew.Close
dbsNorthwind.Close
wrkJet.Close
End Sub
Example (Microsoft Access)
The following example shows three ways to return a Database object in Microsoft Access. The procedure returns a Database object representing the current database, which is open in the Microsoft Access window. Next, the procedure creates another database called Newdb.mdb and saves it to disk. Then it opens an existing database called Another.mdb. Finally, it enumerates all Database objects in the Databases collection.
Sub ReferenceDatabases()
Dim wsp As Workspace
Dim dbsCurrent As Database, dbsNew As Database
Dim dbsAnother As Database, dbs As Database
' Return reference to current database.
Set dbsCurrent = CurrentDb
' Return reference to default workspace.
Set wsp = DBEngine.Workspaces(0)
' Create new Database object.
Set dbsNew = wsp.CreateDatabase("Newdb.mdb", dbLangGeneral)
' Open database other than current database.
Set dbsAnother = wsp.OpenDatabase("Another.mdb")
' Enumerate all open databases.
For Each dbs in wsp.Databases
Debug.Print dbs.Name
Next dbs
For Each dbs In wsp.Databases
Set dbs = Nothing
Next dbs
Set wsp = Nothing
End Sub