Home

Creating a Database With Access.Application

 

Description

To support the creation of a database, the Access.Application object is equipped with a method named NewCurrentDatabase. Its syntax is:

Access.Application.NewCurrentDatabase(filepath, _
				      FileFormat, _
				      Template, _
				      SiteAddress, _
				      ListID)

Only the first argument is required. You can pass it as the name of the new database. Here is an example:

Private Sub cmdCreateDatabase_Click()
    Dim ROSH As Access.Application
    
    Set ROSH = CreateObject("Access.Application")
    ROSH.NewCurrentDatabase "Red Oak High School"
End Sub

If you do not specify an extension, the database engine would find out the latest version of Microsoft Access that is installed in your computer. If it is Microsoft Access 2007, a new database with extension .accdb would be created. Still, if you want, you can add an extension. If you have Microsoft Access 2007 but want to specify the version of database you want to create, pass the second argument. The second argument is a member of the AcNewDatabaseFormat enumeration. The available values are:

AcNewDatabaseFormat Member Constant Value Description
acNewDatabaseFormatUserDefault 0 The database engine will use the current installation of Microsoft Access
acNewDatabaseFormatAccess2000 9 A Microsoft Access database compatible with versions 2.0, 95, 97, or 2000 will be created with the extension .mdb
acNewDatabaseFormatAccess2002 10 A Microsoft Access 2002-2003 compatible database will be created with the extension .mdb
acNewDatabaseFormatAccess12 12 A Microsoft Office Access 2007 compatible database will be created with the extension .accdb

Here is an example:

Private Sub cmdCreateDatabase_Click()
    Dim ROSH As Access.Application
    
    Set ROSH = CreateObject("Access.Application")
    ROSH.NewCurrentDatabase "Red Oak High School", acNewDatabaseFormatAccess2007
End Sub

The other arguments are optional.

After using a database, you should (must) close it to free the resources it was using. To close a database using the Microsoft Office 12.0 Access Database Engine Library, call the CloseCurrentDatabase() method. Its syntax is:

Access.Application.CloseCurrentDatabase()

This method takes no argument. After calling this method, set its variable to Nothing. Here is an example:

Private Sub cmdCloseDatabase_Click()
    Dim ROSH As Access.Application
    
    Set ROSH = CreateObject("Access.Application")
    ROSH.CloseCurrentDatabase
    
    Set ROSH = Nothing
End Sub

When calling this method, the database must have been previously opened. If the database was not opened or it was already closed, you would receive a 2467 error.

Error

 

 

 

 

Home Copyright © 2009-2016, FunctionX, Inc.