CopyObject Action

You can use the CopyObject action to copy the specified database object to a different Microsoft Access database (.mdb) or to the same database or Access project (.adp) under a new name. For example, you can copy or back up an existing object in another database or quickly create a similar object with a few changes.

The CopyObject action has the following arguments.

To run the CopyObject action in Visual Basic, use the CopyObject method of the DoCmd object. The following example uses the Access DoCmd CopyObject method to copy the Employees table and give it a new name in the current database:

DoCmd.CopyObject, "Employees_SourceFileName", acTable, "Employees_DestinationFileName"

AcObjectType can be one of these AcObjectType constants.

acDataAccessPage
acDefault default
acDiagram
acForm
acFunction
acMacro
acModule
acQuery
acReport
acServerView
acStoredProcedure
acTable
 

Action argument Description
Destination Database A valid path and file name for the destination database. Enter the path and file name in the Destination Database box in the Action Arguments section of the Macro window. Leave this argument blank if you want to select the current database.

Note  This argument is only available in the Access database environment (.mdb). When using this action in an Access project environment (.adp), you must leave the destination object argument blank.

If you run a macro containing the CopyObject action in a library database and leave this argument blank, Access will copy the object into the library database.

New Name A new name for the object. When copying to a different database, leave this argument blank to keep the same name.
Source Object Type The object type you want to copy. Click Table, Query, Form, Report, Macro, Module, Data Access Page, Server View, Diagram, Stored Procedure, or Function. To copy the object selected in the Database window, leave this argument blank.
Source Object Name The name of the object to be copied. The Source Object Name box shows all objects in the database of the type selected by the Source Object Type argument. In the Source Object Name box, click the object to copy. If you leave the Source Object Type argument blank, leave this argument blank also.

If you run a macro containing the CopyObject action in a library database, Access looks for the object with this name first in the library database, then in the current database.

Remarks: You must enter a value for either one or both of the Destination Database and New Name arguments for this action.

If you leave the Source Object Type and Source Object Name arguments blank, Access copies the object selected in the Database window. To select an object in the Database window, you can use the SelectObject action with the In Database Window argument set to Yes.

The CopyObject action is similar to selecting an object in the Database window, clicking Copy on the Edit menu, and then clicking Paste on the Edit menu. The Paste As dialog box appears so that you can give the object a new name. The CopyObject action performs all of these steps automatically.