Home

Opening a Database With DAO

 

Opening a Database Using the Workspace

To open a database in DAO, the Workspace class is equipped with a method named OpenDatabase. Its syntax is:

 
Public Function Workspace.OpenDatabase(ByVal Name As String, _
				       ByVal Options As Boolean, _
				       ByVal ReadOnly As Boolean, _
				       ByVal Connect As String) As Database

Only the first argument is required and it is passed as string. You can pass the name of the database file with its extension. Here is an example:

Private Sub cmdOpenDatabase_Click()
    Dim dbExample As Database
    
    Rem Open the Example.accdb database and get a reference to it
    Set dbExample = DBEngine.Workspaces(0).OpenDatabase "Example.accdb"
    
    . . . Use the dbExample database as you see fit

    dbExercise.Close
End Sub

When opening the database, you can lock it so that other people or applications cannot access it at the same time with you. To prevent other items (they are called processes) from accessing the database, pass a second argument as True. On the other hand, you may want to allow other people or applications to be able to access the same database. To specify this option, pass the second argument as False.

Like the second argument, the third argument is optional. If you are opening the database and want to do something on it, such as modifying it, pass the third argument as False. If you don't want to perform any modification action on the database, pass the third argument as True.

The fourth argument allows you to provide connection information.

Opening a Database Using DBEngine

To open a database, you can call the OpenDatabase() method of the DBEngine class. Its syntax is:

Public Function DBEngine.OpenDatabase(ByVal Name As String, _
			              ByVal Options As Boolean, _
				      ByVal ReadOnly As Boolean, _
				      ByVal Connect As String) As Database

As you can see, the arguments of this method are exactly the same as those of the Workspace class of the Microsoft Access Object library. This means that the arguments follow the same description we saw already. Here is an example of calling the method:

Private Sub cmdOpenDatabase_Click()
    
    OpenDatabase("Example.accdb")
    
End Sub

When the DBEngine.OpenDatabase() method has been called, it returns a Database object. If you want to continue doing anything on the open database, you must get this Database value. Here is an example of getting it:

Private Sub cmdOpenDatabase_Click()
    Dim db As DAO.Database

    Set db = OpenDatabase("Example.accdb")
    
    . . . Now you can use the Database object

    db.Close

End Sub

Remember that the other arguments of the DBEngine.OpenDatabase() method follow the same description we saw for the Workspace class.

 
 
     
 

Home Copyright © 2009-2016, FunctionX, Inc.