Friday, November 9, 2018

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

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

You use a command button on an Access 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 command button to other forms without losing the functionality of the button.

What do you want to do?

Add a command button to a form by using a wizard

Create a button by dragging a macro to a form

Create a command button without using a wizard

Customize a command button

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

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

    Button image

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

    The Command Button Wizard starts.

  5. 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 Builder button 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.

  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.

    Access automatically creates a command button and uses the macro name as the button's caption. Access 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 user interface 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.

  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

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

    Button image

  4. 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.

  5. With the command button selected, press F4 to display its property sheet.

  6. Click the All tab to display all the properties for the command button.

  7. 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 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:

  • To make the command button run an existing saved macro, click the drop-down arrow and click the macro name.

  • To make the command button run an existing built-in function or VBA function, type an equal sign (=) followed by the function name; for example, =MsgBox("Hello World") or =MyFunction(arguments).

  • To build an expression, click Builder button and then click Expression Builder.

  • To begin building an event procedure that uses Visual Basic for Applications (VBA) code, click Builder button and then click Code Builder.To build a new embedded macro containing the actions that you want the command button to perform, click Builder button and then click Macro Builder.

    Note: Access 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

Access provides many 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

      3. 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

      4. 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. Select the command button.

        2. Click either Move Up or Move Down on the Arrange tab.

          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 and Move Down commands are disabled for stacked layouts.

      5. 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.

        2. Drag the layout selector to move the layout to a new location.

    • Add gridlines to a layout of command buttons    

      1. Select any of the command buttons in the layout.

      2. Click the Gridlines command on the Arrange tab, 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.

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

        Commands available in the Access Font group

    • 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. 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 Builder button 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    

      You can 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

No comments:

Post a Comment