Create or modify a command button

Microsoft Access can create your command button for you if you use a wizard. However, wizards are not available in a stand-alone data access page, or when you point a data access page to a database other than the one that is currently open. The Command Button Wizard is also not available if the data access page isn't bound to a table or query. You will have to create a command button on your own if wizards aren't available.

Create or modify a command button with a wizard

  1. Open a form or a data access page in Design view.
  2. Make sure the Control Wizards tool in the toolbox is pressed in.
  3. In the toolbox, click the Command Button Command Button controltool.
  4. On the form or data access page, click where you want to place the command button.
  5. Follow the directions in the wizard dialog boxes. In the last dialog box, click Finish to display the command button in Design view.

Create or modify a command button without a wizard

  1. Open a form or a data access page in Design view.
  2. Make sure the Control Wizards tool in the toolbox is not pressed in. This turns off the wizard.
  3. In the toolbox, click the Command Button tool Command Button control.
  4. On the form or data access page, click where you want to place the command button.
  5. Make sure that the command button is selected, and then click Properties on the toolbar to open the command button's property sheet.
  6. In the OnClick property box on a form, enter the name of the macro or event procedure that you want to run when the button is clicked, or click the Build button to use the Macro Builder or Code Builder. On a data access page, you can attach code written in either JScript or Microsoft Visual Basic Scripting Edition (VBScript) to a command button by using the Microsoft Script Editor.
  7. Do one of the following:

    To display text on a command button on a data access page: Type the text in command button's Value property box.

    To display text on a command button on a form: Type the text in the command button's Caption property box.

    To display a picture on a command button on a form: In the command button's Picture property box, type the path and file name for a .bmp, .ico, or .dib file. If you're not sure of the path or file name, click the Build button to open the Picture Builder.

Tip

On a form, to Create or modify a command button that runs a macro, drag the macro from the Database window to form Design view.

Create a Cancel button

  1. Open a form in Design view.
  2. Click the command button, and then click Properties on the toolbar to open the command button's property sheet.
  3. 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, you can choose the command button by clicking it, pressing the ESC key, or pressing ENTER when the command button has the focus. When the Cancel property is set to Yes for one command button, it's automatically set to No for all other command buttons on the form. Only one button can be the default on a form.

To have 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 OnClick property of the button.

Tip  

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

See an event procedure created by the Command Button Wizard

The following procedure describes how to display an event procedure created by the Command Button Wizard on a form. In a data access page, you have to create the event procedure yourself.

  1. In form Design view, right-click the command button created by the Command Button Wizard.
  2. On the shortcut menu, click Build Event.