Applies To DBEngine object, Workspace object.
Description
Opens a specified database in a Workspace object and returns a reference to the Database object that represents it.
Syntax Set database = workspace.OpenDatabase(dbname, options, read-only, connect) The OpenDatabase method syntax has these parts.| Part | Description | 
| database | An object variable that represents the Database object that you want to open. | 
| workspace | Optional. An object variable that represents the existing Workspace object that will contain the database. If you don't include a value for workspace, OpenDatabase uses the default workspace. | 
| dbname | A String that is the name of an existing Microsoft Jet database file, or the data source name (DSN) of an ODBC data source. See the Name property for more information about setting this value. | 
| options | Optional. A Variant that sets various options for the database, as specified in Settings. | 
| read-only | Optional. A Variant (Boolean subtype) value that is True if you want to open the database with read-only access, or False (default) if you want to open the database with read/write access. | 
| connect | Optional. A Variant (String subtype) that specifies various connection information, including passwords. | 
| Setting | Description | 
| True | Opens the database in exclusive mode. | 
| False | (Default) Opens the database in shared mode. | 
| Constant | Description | 
| dbDriverNoPrompt | The ODBC Driver Manager uses the connection string provided in dbname and connect. If you don't provide sufficient information, a run-time error occurs. | 
| dbDriverPrompt | The ODBC Driver Manager displays the ODBC Data Sources dialog box, which displays any relevant information supplied in dbname or connect. The connection string is made up of the DSN that the user selects via the dialog boxes, or, if the user doesn't specify a DSN, the default DSN is used. | 
| dbDriverComplete | (Default) If the connect and dbname arguments include all the necessary information to complete a connection, the ODBC Driver Manager uses the string in connect. Otherwise it behaves as it does when you specify dbDriverPrompt. | 
| dbDriverCompleteRequired | This option behaves like dbDriverComplete except the ODBC driver disables the prompts for any information not required to complete the connection. | 
Remarks When you open a database, it is automatically added to the Databases collection. Further, in an ODBCDirect workspace, the Connection object corresponding to the new Database object is also created and appended to the Connections collection of the same Workspace object.
Some considerations apply when you use dbname:"FoxPro 2.5; pwd=mypassword"See Also Close method, Connect property.
Specifics (Microsoft Access) In Microsoft Access, use the CurrentDb function to return a reference to the current database and assign it to a Database variable, as shown in the following example.Dim dbsCurrent As Database
Set dbsCurrent = CurrentDbSub OpenDatabaseX()
    Dim wrkJet As Workspace
    Dim dbsNorthwind As Database
    Dim dbsPubs As Database
    Dim dbsPubs2 As Database
    Dim dbsLoop As Database
    Dim prpLoop As Property
    ' Create Microsoft Jet Workspace object.
    Set wrkJet = CreateWorkspace("", "admin", "", dbUseJet)
    ' Open Database object from saved Microsoft Jet database
    ' for exclusive use.
    MsgBox "Opening Northwind..."
    Set dbsNorthwind = wrkJet.OpenDatabase("Northwind.mdb", True)
    ' Open read-only Database object based on information in
    ' the connect string.
    MsgBox "Opening pubs..."
    Set dbsPubs = wrkJet.OpenDatabase("Publishers", _
        dbDriverNoPrompt, True, _
        "ODBC;DATABASE=pubs;UID=sa;PWD=;DSN=Publishers")
    ' Open read-only Database object by entering only the
    ' missing information in the ODBC Driver Manager dialog
    ' box.
    MsgBox "Opening second copy of pubs..."
    Set dbsPubs2 = wrkJet.OpenDatabase("Publishers", _
        dbDriverCompleteRequired, True, _
        "ODBC;DATABASE=pubs;DSN=Publishers;")
    ' Enumerate the Databases collection.
    For Each dbsLoop In wrkJet.Databases
        Debug.Print "Database properties for " & _
            dbsLoop.Name & ":"
        On Error Resume Next
        ' Enumerate the Properties collection of each Database
        ' object.
        For Each prpLoop In dbsLoop.Properties
            If prpLoop.Name = "Connection" Then
                ' Property actually returns a Connection object.
                Debug.Print "    Connection[.Name] = " & _
                    dbsLoop.Connection.Name
            Else
                Debug.Print "    " & prpLoop.Name & " = " & _
                    prpLoop
            End If
        Next prpLoop
        On Error GoTo 0
    Next dbsLoop
    dbsNorthwind.Close
    dbsPubs.Close
    dbsPubs2.Close
    wrkJet.Close
End SubSub 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 SubDim a(100), db As Database
i = 0
ChDrive "C"
ChDir Application.Path
a(i) = Dir("*.MDB")
If a(i) = "" Then
    MsgBox "You have no databases in the Microsoft Excel folder"
    Exit Sub
End If
Do
    i = i + 1
    a(i) = Dir()
Loop Until a(i) = ""
Set theDialog = DialogSheets.Add
Set list1 = theDialog.ListBoxes.Add(78, 42, 84, 80)
For counter = 0 To i - 1
    list1.AddItem a(counter)
Next
Application.ScreenUpdating = True
theDialog.Show
Set db = Workspaces(0).OpenDatabase(a(list1.Value - 1))
MsgBox "The " & db.Name & " database is now open"
' use database here
db.Close