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 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.
- Right-click the form in the Navigation Pane, and then click Design view on the shortcut menu.
- On the Design tab, in the Controls group, ensure that Use Control Wizards is selected.
- On the Design tab, in the Controls group, click Button.
- In the design grid, click where you want the command button to be inserted.
The Command Button Wizard starts.
- 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
- In the Navigation Pane, right-click the form that contains the command button, and then click Design View or Layout View on the shortcut menu.
- Click the command button to select it, and then press F4 to display its property sheet.
- 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 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
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.
- Open the form in Design view by right-clicking the form in the Navigation Pane, and then clicking Design View on the shortcut menu.
- 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
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.
- Right-click the form in the Navigation Pane, and then click Design View on the shortcut menu.
- On the Design tab, in the Controls group, ensure that Use Control Wizards is not selected.
- On the Design tab, in the Controls group, click Button.
- 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.
- With the command button selected, press F4 to display its property sheet.
- Click the All tab to display all the properties for the command button.
- Set the properties to complete the design of the command button, as shown in the following table.
||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.
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.
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.
- For help with other properties not mentioned here, place the cursor in a property box and press F1.
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.
- Right-click the form in the Navigation Pane, and then click Design View .
- 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.
- Right-click one of the command buttons that you want to add to the layout, and then point to Layout on the shortcut menu.
- Click Tabular to start a horizontal row of buttons, or click Stacked 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.
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.
- 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.
When you release the mouse button, the control is added to the 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:
- Right-click the command button, and then point to Layout on the shortcut menu.
- Click either Move Up a Section or Move Down a Section .
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
- Click any of the command buttons in the layout.
A layout selector appears at the upper left corner of the layout.
- Drag the layout selector to move the layout to a new location.
- Add gridlines to a layout of command buttons
- Right-click any of the command buttons in the layout and, on the shortcut menu, point to Layout.
- 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.
- Click the command button you want to make transparent, and then press F4 to display the command button's property sheet.
- On the Format tab of the property sheet, click in the Transparent property box.
- 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.
- Click the command button to select it and press F4 to display its property sheet.
- On the Format tab of the property sheet, click in the Back Style property box.
- In the drop-down list, select Transparent.
The body of the command button is hidden, but its caption remains visible.
- To underline or change the color of the text in the caption, use the tools in the Font group on the Design tab.
- Click the command button, and then press F4 to open its property sheet.
- 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 to open the 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:
- Add a picture to the command button by using the procedure outlined earlier in this section.
- Select the command button. If the property sheet is not already displayed, press F4 to display it.
- On the Format tab of the property sheet, type the caption you want in the Caption property box.
- 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