Create or modify a command button

Microsoft Access can create your command button (command button: A control that runs a macro, calls a Visual Basic function, or runs an event procedure. A command button is sometimes called a push button in other programs.) for you if you use a wizard.

ShowIf the wizard doesn't start

This could be due to the fact that Access is running in sandbox mode but Microsoft Jet 4.0 SP8 or later is not installed on your computer. Jet 4.0 SP8 or later is required for Access to be fully functional when sandbox mode is enabled.

For more information about installing the Jet upgrade, see the Office Online article About Microsoft Jet 4.0 SP8 or later.

For more information about sandbox mode, see the Office Online article About Microsoft Jet Expression Service sandbox mode.

Also, 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.

ShowCreate or modify a command button with a wizard

  1. Open a form or a data access page in Design view (Design view: A view that shows the design of these database objects: tables, queries, forms, reports, and macros. In Design view, you can create new database objects and modify the design of existing objects.).
  2. Make sure the Control Wizards Button image tool in the toolbox is pressed in.
  3. In the toolbox, click the Command Button Control tool.
  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.

ShowCreate or modify a command button without a wizard

  1. Open a form or a data access page in Design view (Design view: A view that shows the design of these database objects: tables, queries, forms, reports, and macros. In Design view, you can create new database objects and modify the design of existing objects.).
  2. Make sure the Control Wizards Button image tool in the toolbox is not pressed in. This turns off the wizard.
  3. In the toolbox, click the Command Button tool 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 Button image on the toolbar to open the command button's property sheet (property sheet: A pane that is used to view or modify the properties of various objects such as tables, queries, fields, forms, reports, data access pages, and controls.).
  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 (JavaScript: A scripting language that can be used to add functionality to a Web page or a Web site, or to enhance the look of one. JavaScript scripts can run on any type of client or server computer.) or Microsoft Visual Basic Scripting Edition (VBScript (Visual Basic Scripting Edition (VBScript): A subset of the Microsoft Visual Basic programming system. At least Microsoft Internet Explorer 3, along with the other Web browsers, can read VBScript programs that are embedded in HTML pages.)) 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 Button image to open the Picture Builder.

ShowTip

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.

ShowCreate a Cancel button

  1. Open a form in Design view (Design view: A view that shows the design of these database objects: tables, queries, forms, reports, and macros. In Design view, you can create new database objects and modify the design of existing objects.).
  2. Click the command button, and then click PropertiesButton image 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 (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 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 (macro: An action or set of actions that you can use to automate tasks.) or event procedure (event procedure: A procedure that is automatically executed in response to an event initiated by the user or program code, or that is triggered by the system.) and attach it to the OnClick property of the button.

ShowTip

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.

ShowSee 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 (Design view: A view that shows the design of these database objects: tables, queries, forms, reports, and macros. In Design view, you can create new database objects and modify the design of existing objects.), right-click the command button created by the Command Button Wizard.
  2. On the shortcut menu, click Build Event.
 
 
Applies to:
Access 2003