Share via


Creating, Opening, and Closing an Access Application

You can create a new database, or open and close an existing database, from within Microsoft® Access® or by using Automation from another application. The methods discussed in this section are typically used in Automation from another application. If your code is running inside Access, the code typically works with the currently open database, and using these methods is not necessary.

Note   If you are working in another application and you must access only the data in a database (tables or queries), and not objects such as forms or reports, you use ADO to access the data you require.

You use the NewCurrentDatabase method to create a new .mdb-type database. You use the OpenCurrentDatabase and CloseCurrentDatabase methods to open and close an existing .mdb-type database. The following sample is designed to be run from any Microsoft® Office application. It opens the Northwind Traders sample database and prints the portion of the Product Catalog report specified in the OpenReport method:

Sub PrintReport(strCategoryName As String)
   Dim acApp         As Access.Application
   Dim strDBPath     As String

   Const DB_PATH As String = _
      "c:\program files\microsoft office\office\samples\northwind.mdb"

   Set acApp = New Access.Application
   With acApp
      .OpenCurrentDatabase DB_PATH
      ' Print the Product Catalog report.
      .DoCmd.OpenReport "Catalog", acViewNormal, , _
         "CategoryName = '" & strCategoryName & "'"
   End With
   acApp.Quit
   Set acApp = Nothing
End Sub

You use the NewAccessProject, OpenAccessProject, or CreateAccessProject method to open or create an .adp-type database. The NewAccessProject method creates a new .adp-type database and causes it to become active, whereas the CreateAccessProject method only creates an .adp file on disk. You use the OpenAccessProject method to open an existing .adp-type database and the CloseCurrentDatabase method to close an .adp-type database.

When you create a new database or have a database open, you can use other methods of the Application object to create new Access objects. For example, you use the CreateForm and CreateControl methods to create forms and controls on forms. You use the CreateReport and CreateReportControl methods to create reports and controls on reports. You use the CreateDataAccessPage method to create data access pages. To programmatically add controls to a data access page, you must use script or the Dynamic HTML (DHTML) object model to work with HTML directly.

Note   Although the methods discussed above let you programmatically create a database and the objects it contains, these methods typically are used only in wizards or add-ins. Generally, you create the database and its objects through the Access user interface and then work with these objects programmatically by using Microsoft® Visual Basic® for Applications (VBA) code run from Access or another Office application.

See Also

Built-in Access Functions and Methods | The CurrentData and CurrentProject Objects | Working with the Screen Object | Working with the DoCmd Object | Working with the Modules Collection | Working with the References Collection | Add-ins, Templates, Wizards, and Libraries