Edit

Share via


Create a database template programmatically

The SaveAsTemplate method enables you to convert an existing Access database file to a database template (.accdt) format file that can be featured on the Getting Started with Microsoft Office Access page.

Syntax

expression. SaveAsTemplate(TemplateLocation, TemplateName, PreviewImage, Description, Category, Keywords, Identifier, Reserved)

expression A variable that represents a TemplateCreator object.

The following table describes the arguments of the SaveAsTemplate method.

Name Required/Optional Data type Description
TemplateLocation Required String The full path and file name of the database template to create.For the template to appear on the Getting Started with Microsoft Office Access page, it must be saved to Microsoft\Templates subfolder of the user's Application Data folder.

In Windows XP, the default location of the Application Data folder is C:\Documents and Settings\ User Name \Application Data, where User Name is the name of the user who is currently logged on.

In Windows Vista, the default location of the Application Data folder is C:\Users\ User Name \AppData\Roaming, where User Name is the name of the user who is currently logged on. Use the Environ function to determine the current location of the user's Application Data folder. The following code illustrates how to do this. strTemplateLocation = Environ("AppData") & "\Microsoft\Templates\"
TemplateName Optional String The name of the database that is created when the user opens the template.
PreviewImage Optional String An image file to be used as a preview for the database template on the Getting Started with Microsoft Office Access page.
Description Optional String A description to be displayed when the user selects the database template in the Getting Started with Microsoft Office Access page.
Category Optional String The Template Category under which the database template will appear on the Getting Started with Microsoft Office Access page.
Keywords Optional String Keywords to be added to the template's file properties.
Identifier Optional String
Reserved Optional String

Usage

You must instantiate the TemplateCreator object before you call the SaveAsTemplate method. Instantiating the TemplateCreator object requires a different technique from instantiating the built-in objects in Access. To instantiate the TemplateCreator object, you must use the COMAddins collection.

The following code illustrates how to instantiate the AccessDeveloperExtensions object through the COMAddins collection.

Set objTemplate = Application.COMAddIns("AccessAddIn.ADE").Object.TemplateObject 

The following example creates a new template named Asset Tracker and assigns it to the Departmental data category on the Getting Started with Microsoft Office Access page.

    Dim objTemplate As TemplateCreator 
    Dim strTemplateLocation As String 
     
    ' The database template must be saved to this location to appear on the 
    ' Getting Started with Microsoft Office Access page. 
    strTemplateLocation = Environ("AppData") & "\Microsoft\Templates\" 
     
    ' Instantiate a TemplateObject object. 
    Set objTemplate = Application.COMAddIns("AccessAddIn.ADE").Object.TemplateObject 
 
    ' Create the database template.     
    objTemplate.SaveAsTemplate TemplateLocation:=strTemplateLocation & "AssetTracker.accdt", _ 
                               TemplateName:="Asset Tracker", _ 
                               Category:="Departmental Data"

You must set a reference to the Access Developer Extensions type library in order to use the SaveAsTemplate method. To do this, follow these steps:

  1. On the Tools menu, click References.
  2. Select the Microsoft Office Access Developer Extensions Type Library 1.0 check box, and then click OK.

Support and feedback

Have questions or feedback about Office VBA or this documentation? Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.