Wednesday, July 7, 2021

Copyobject macro action

You can use the CopyObject action to copy the specified database object to a different Access database or to the same database 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.

Note: This action won't be allowed if the database isn't trusted. For more information about enabling macros, see Decide whether to trust a database.

Note: The CopyObject macro action isn't available in Access web apps.

Setting

The CopyObject action has the following arguments.

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 Builder window. Leave this argument blank if you want to select the current database.

Note: This argument is only available in the Access desktop database environment. When using this action in an Access project environment (.adp) with Access 2007 and Access 2010, the Destination Database argument must be blank.

If you run a macro containing the CopyObject action in a library database and leave this argument blank, Access copies 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 Ob ject 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 Navigation Pane, 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, select 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 first looks for the object with this name in the library database, and 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 Navigation Pane. To select an object in the Navigation Pane, you can use the SelectObject action with the In Navigation Pane argument set to Yes.

The CopyObject action is similar to performing the following steps manually:

  1. Select an object in the Navigation Pane.

  2. On the Home tab, in the Clipboard group, click Copy.

  3. On the same tab, click Paste.

    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.

The path and file name of the destination database must exist before the macro runs the CopyObject action. If they don't exist, Access displays an error message.

To run the CopyObject action in a Visual Basic for Applications (VBA) module, use the CopyObject method of the DoCmd object.

You can also manually copy an object selected in the Navigation Pane, or an object that is currently open, by clicking File > Save As. This command will make a copy of the object in the current database only. In the Save As dialog box, enter the name for the copy, and choose what type of object you want to save it as. If the original object has already been saved and you save it in the current database with a new name, the original version still exists with its old name.

To manually copy an object to a different Access database:

  1. On the External Data tab, in the Export group, click More and then click Access Database.

  2. In the Export - Access Database dialog box, enter the file name of the destination database.

    -or-

    Click Browse to display the File Save dialog box, locate the destination database, and then click Save.

  3. In the Export - Access Database dialog box, click OK. The Export dialog box appears.

  4. In the Export dialog box, enter a name for the object in the destination database. Choose any applicable options, such as Export Definition and Data or Definition Only for tables. When you are finished, click OK.

No comments:

Post a Comment