Use a command button to start an action or a series of actions

You use a command button on a form to start an action or a set of actions. For example, you can create a command button that opens another form. To make a command button perform an action, you write a macro or event procedure and attach it to the command button's On Click property. You can also embed a macro directly into the On Click property of the command button. This makes it possible to copy the button to other forms without losing the functionality of the button.

 Tip    Try Office 2010 Access 2010 has a new Macro Designer that makes it easier to create, edit, and automate database logic. What a video or try Office 2010.

What do you want to do?


Add a command button to a form by using a wizard

By using the Command Button Wizard, you can quickly create command buttons that do a variety of tasks, such as closing the form, opening a report, finding a record, or running a macro.

  1. Right-click the form in the Navigation Pane, and then click Design view on the shortcut menu.
  2. On the Design tab, in the Controls group, ensure that Use Control Wizards Button image is selected.

Button image

  1. On the Design tab, in the Controls group, click Button.

Button image

  1. In the design grid, click where you want the command button to be inserted.

The Command Button Wizard starts.

  1. Follow the directions in the wizard. On the last page, click Finish.

The wizard creates the command button and embeds a macro in the button's On Click property. The macro contains actions that perform the task you chose in the wizard.

View or edit a macro embedded in a command button

  1. In the Navigation Pane, right-click the form that contains the command button, and then click Design View Button image or Layout View Button image on the shortcut menu.
  2. Click the command button to select it, and then press F4 to display its property sheet.
  3. On the Event tab of the property sheet, [Embedded Macro] should be displayed in the On Click property box. Click anywhere in the property box, and then click Button image in the right side of the box.

The Macro Builder is displayed, showing the action or actions that make up the embedded macro.

Top of Page Top of Page

Create a button by dragging a macro to a form

If you have already created and saved a macro, you can easily create a command button that runs the macro by dragging the macro from the Navigation Pane to a form that is open in Design view.

  1. Open the form in Design view by right-clicking the form in the Navigation Pane, and then clicking Design View Button image on the shortcut menu.
  2. In the Navigation Pane, locate the macro that you want the new command button to run, and then drag the macro to the form.

Microsoft Office Access 2007 automatically creates a command button and uses the macro name as the button's caption. Office Access 2007 also inserts the macro name in the On Click property of the command button so that the macro runs when you click the button. Access uses a generic name for the button, so it is a good idea to type a more meaningful name in the button's Name property. To display the property sheet for the command button while the form is open in Design view, click the button, and then press F4.

For more information about creating macros, see the article Create a macro.

Top of Page Top of Page

Create a command button without using a wizard

You can create a command button without using the Command Button Wizard. The process involves placing the button on the form and then setting a few properties.

  1. Right-click the form in the Navigation Pane, and then click Design View Button image on the shortcut menu.
  2. On the Design tab, in the Controls group, ensure that Use Control Wizards is not selected.

Button image

  1. On the Design tab, in the Controls group, click Button.

Button image

  1. Click the location on the form where you want to place the command button.

Access places the command button on the form.

Because Use Control Wizards was not selected, Access does not perform any further processing. If the Command Button Wizard starts when you place the command button on the form, you can click Cancel in the wizard.

  1. With the command button selected, press F4 to display its property sheet.
  2. Click the All tab to display all the properties for the command button.
  3. Set the properties to complete the design of the command button, as shown in the following table.
Property Description
Name By default, Access gives a new command button the name Command, followed by a number; for example, Command34. Although it is not required, it is a good idea to change the name to something that reflects the function of the button (for example, cmdSalesReport or CloseFormButton). This helps you later, if you need to refer to the button in a macro or event procedure.
Caption

Type the label that that you want displayed on the command button. For example, Sales Report or Close Form.

 Note   The caption is not displayed if a picture is specified in the Picture property.

On Click

Use this property to specify what happens when the command button is clicked. To set the property, click in the property box and then do one of the following:

 Note   Office Access 2007 databases which contain VBA code must be granted trusted status before the code can be run.

  1. For help with other properties not mentioned here, place the cursor in a property box and press F1.

Top of Page Top of Page

Customize a command button

Office Access 2007 provides new ways to customize command buttons so that you can have the appearance and functionality you want on your form. For example, you can create a row of command buttons in a tabular or stacked arrangement, or you can make command buttons appear more like hyperlinks.

  1. Right-click the form in the Navigation Pane, and then click Design View Button image.
  2. Do one of the following:
    • Create a horizontal (tabular) or vertical (stacked) layout of command buttons    

You can add command buttons to a tabular or stacked layout to create a row or column of precisely aligned buttons. You can then reposition the buttons as a group, which makes it easier to modify forms. You can also apply formatting styles to the entire row or column of command buttons at the same time, and you can separate the buttons by using grid lines.

  1. Right-click one of the command buttons that you want to add to the layout, and then point to Layout on the shortcut menu.
  2. Click Tabular Button image to start a horizontal row of buttons, or click Stacked Button image to create a vertical column of buttons.

Access draws borders around the command button to indicate the layout. Each button is paired with a label that you cannot delete — however, you can shrink the label to a very small size, if it is not needed.

The following illustration shows a command button and its associated label in a tabular layout. Note how the label is in the next higher section — this keeps the labels from repeating for each detail record.

Command button in a tabular layout

The following illustration shows a command button and its associated label in a stacked layout. In a stacked layout, the label and the button are always in the same section.

Command button in a stacked layout

  1. Add more command buttons to the layout by dragging them to the layout area. As you drag a command button over the layout area, Access draws a horizontal insertion bar (for a tabular layout) or a vertical insertion bar (for a stacked layout) to indicate where the command button will be placed when you release the mouse button.

Adding a button to a stacked control layout

When you release the mouse button, the control is added to the layout.

Button added to stacked control layout

  • Move command buttons up a section or down a section within a tabular layout    If you want to keep a command button in a tabular layout but want to move the command button to a different section of the form (for example, from the Detail section to the Form Header section), do the following:
    1. Right-click the command button, and then point to Layout on the shortcut menu.
    2. Click either Move Up a Section Button image or Move Down a Section Button image.

The command button moves up or down to the next section, but remains within the tabular layout. If there is a control already in the position that you are moving this control to, the two controls exchange places.

 Note   The Move Up a Section and Move Down a Section commands are disabled for stacked layouts.

  • Move an entire layout of command buttons    
  1. Click any of the command buttons in the layout.

A layout selector appears at the upper left corner of the layout.

  1. Drag the layout selector to move the layout to a new location.
  • Add gridlines to a layout of command buttons    
  1. Right-click any of the command buttons in the layout and, on the shortcut menu, point to Layout.
  2. Point to Gridlines, and then click the style of gridlines you want.
  • Make a command button transparent    

By making a command button transparent, you can place it over any object on your form and give that object the functionality of a command button. For example, you have an image that you want to divide into separate, clickable areas, each of which starts a different macro. You can do this by placing multiple, transparent command buttons on top of the image.

  1. Click the command button you want to make transparent, and then press F4 to display the command button's property sheet.
  2. On the Format tab of the property sheet, click in the Transparent property box.
  3. Select Yes in the drop-down list.

You can still see the outline of the command button in Design view, but the button is invisible in Form view.

 Note   Setting a command button's Transparent property to Yes is not the same as setting its Visible property to No. Both operations hide the command button, but setting the Transparent property to Yes leaves the button enabled. Setting the Visible property to No disables the button.

  • Make a command button appear as a hyperlink    

If you want, you can hide a command button but leave the caption visible. The result is something that looks like a label, but functions as a command button. You can also underline the text in the caption and change its color to make it appear as a hyperlink.

  1. Click the command button to select it and press F4 to display its property sheet.
  2. On the Format tab of the property sheet, click in the Back Style property box.
  3. In the drop-down list, select Transparent.

The body of the command button is hidden, but its caption remains visible.

  1. To underline or change the color of the text in the caption, use the tools in the Font group on the Design tab.

Access Ribbon Image

  • Create a Cancel button    
  1. Click the command button, and then press F4 to open its property sheet.
  2. In the Cancel property box, click Yes.

When a command button's Cancel property is set to Yes, and the form is the active form, a user can select the command button by clicking it, pressing the ESC key, or pressing ENTER when the command button has focus (focus: The ability to receive user input through mouse or keyboard actions or the SetFocus method. Focus can be set by the user or by the application. The object that has focus is usually indicated by a highlighted caption or title bar.). When the Cancel property is set to Yes for any one command button, that property is automatically set to No for all other command buttons on the form.

To make a Cancel button cancel all the actions that have taken place in a form or dialog box, you need to write a macro or event procedure and attach it to the On Click property of the button.

 Note   For a form that allows irreversible operations (such as deletions), it is a good idea to make the Cancel button the form's default command button. To do this, set both the Cancel property and the Default property to Yes.

  • Display a picture on a command button    In the command button's Picture property box, type the path and file name for a picture file (such as a .bmp, .ico, or .dib file). If you are not sure of the path or file name, click Button image to open the Picture Builder.

Picture Builder

Click Browse to find the picture that you want to use or, alternatively, click one of the pictures in the Available Pictures list to preview the professionally-created pictures that you can use. If you find a picture you want, click OK to add it to the command button.

By default, Access sets the Picture Type property to Embedded. When you assign a graphic to a command button's Picture property, using this setting creates a copy of the picture and stores it in the Access database file. Any subsequent changes to the original picture will not be reflected in the command button. To create a link to the original picture so that any changes to the picture will be reflected in the command button, change the Picture Type property to Linked. You must keep the original picture file in its original location. If you move or rename the picture file, Access displays an error message when you open the database, and the command button displays its caption instead of the picture.

  • Display both a picture and a caption on a command button    

As a new feature of Office Access 2007, you can now display both a caption and a picture on a command button. Use the following procedure:

  1. Add a picture to the command button by using the procedure outlined earlier in this section.
  2. Select the command button. If the property sheet is not already displayed, press F4 to display it.
  3. On the Format tab of the property sheet, type the caption you want in the Caption property box.
  4. Click the drop-down arrow in the Picture Caption Arrangement property box, and then select the arrangement you want. For example, to display the caption below the picture, select Bottom. To display the caption to the right of the picture, select Right. To make the arrangement dependent on the system locale, select General. By using this setting, the caption will appear on the right for languages that read from left to right, and it will appear on the left for languages that read from right to left.

Top of Page Top of Page

 
 
Applies to:
Access 2007