Home

Creating a Database in DAO

 

Description

To create a database in DAO, call the CreateDatabase() method of the the DBEngne class. Its syntax is:

DBEngine.CreateDatabase(ByVal Name As String, _
			ByVal locale As String, _
			ByVal options As Variant) As Database

Because the DBEngine object is already recognized in the current database, you can omit it when calling the CreateDatabase() method. When the CreateDatabase() method has finished, it returns a reference to the database that was created and you must obtain that reference.

The database is recognized as the Database object of DAO. To get it, first declare a variable of type Database. Because the DAO class is implied, you can omit it in your declaration and simply use the Database object as type.

To get a reference to the new database, use the SET operator and assign the returned value of the method to your Database variable. Here is an example that creates a new database named Exercise.accdb in the current folder:

Private Sub cmdCreate_Click()
    Dim db As DAO.Database

    Set db = CreateDatabase("Exercise.accdb", dbLangGeneral)
End Sub

After using a database, you should close it to release the resources it was consuming. Here is an example:

Private Sub cmdUseDatabase_Click()
    Dim db As DAO.Database

    Set db = OpenDatabase("Example.accdb")

    . . . Now you can use the Database object

    db.Close
End Sub
     
 

Home Copyright © 2009-2016, FunctionX, Inc.