Lesson 35 of 50 – Command Buttons in MS Access
70%

Command Buttons in MS Access

A Command Button is a form control that performs an action when a user clicks it. In Microsoft Access, command buttons can open forms, open reports, close forms, find records, print reports, and perform other database operations.

1. What Is a Command Button?

A Command Button allows users to perform an operation without navigating through menus manually. You can place buttons on forms to make a database easier to use.

Example: Student Management Form
Student Management
Illustrative example only. These buttons are disabled and do not perform Microsoft Access actions.
Uses of Command Buttons
  • Open another form.
  • Open a report.
  • Close the current form.
  • Find a specific record.
  • Print a report.
  • Run a macro or VBA procedure.

2. Creating a Command Button

Follow these steps to create a command button:

  1. Open your Access database.
  2. Open the required form in Design View.
  3. Select the Design tab on the ribbon.
  4. Make sure Use Control Wizards is enabled if you want guided setup.
  5. Select the Button control from the Controls group.
  6. Click or drag on the form to place the button.
  7. If the Command Button Wizard opens, select the action you want.
  8. Choose whether to display text or a picture on the button.
  9. Enter a meaningful name for the button if prompted.
  10. Finish the wizard and save the form.
  11. Open the form in Form View and test the button.
Tip: If the Command Button Wizard does not appear, check whether Use Control Wizards is enabled on the Design tab.

3. Types of Command Button Actions

The Command Button Wizard provides several common actions. The available choices depend on the form and Access version.

Action Purpose Example Button
Open Form Opens another form. Student Details
Open Report Opens a report for viewing or printing. View Fee Report
Close Form Closes the current form. Close
Find Record Helps locate a record. Find Student
Print Report Prints a selected report. Print Fees Report
Run Macro or VBA Executes configured automation or code. Custom Action

4. Opening Another Form

You can use a command button to open another form. For example, a main menu form can contain a button that opens the student details form.

Example

Suppose your database contains these forms:

  • frmMainMenu
  • frmStudents
  • frmCourses
Using Command Button Wizard
  1. Open frmMainMenu in Design View.
  2. Add a Command Button.
  3. In the wizard, choose the Form Operations category.
  4. Select the action to open a form.
  5. Choose frmStudents.
  6. Choose whether to display all records or matching records, where offered.
  7. Set the button text to Open Students.
  8. Finish and save the form.
Using a Macro

You can also configure the button's On Click event to run a macro with the OpenForm action. Select the target form and configure any required arguments.

Result: Clicking the button in Form View opens the configured student form.

5. Closing a Form

A Close button closes the current form. It is commonly placed on admission forms, data-entry forms, and database navigation menus.

Using Command Button Wizard
  1. Open the form in Design View.
  2. Add a Command Button.
  3. Choose the Form Operations category in the wizard.
  4. Select the action to close the form.
  5. Choose a text label such as Close Form.
  6. Finish and save the form.
Using a Macro

Configure the button's On Click event to run a macro with the CloseWindow action. Select the appropriate object type and object name if required.

Test the button in Form View to make sure it closes the intended form.

6. Opening a Report

A command button can open a report so users can view summaries such as student lists, fee details, or course information.

Example Reports
  • rptStudentList
  • rptFeeDetails
  • rptCourseSummary
Steps
  1. Open the form in Design View.
  2. Add a Command Button.
  3. Choose the Report Operations category in the wizard.
  4. Select the action to preview or open a report.
  5. Select the required report.
  6. Set the button caption to View Report.
  7. Finish and save the form.

You can configure a separate button for printing when that action is available in the wizard.

7. Changing Button Text and Appearance

You can customize a command button to make its purpose clear to users.

Important Properties
Property Purpose
Name Identifies the button control.
Caption Specifies the text displayed on the button.
Picture Specifies an image displayed on the button, where supported.
Enabled Determines whether the button can be used.
Visible Determines whether the button is displayed.
On Click Specifies the action or event procedure to run when clicked.
Steps to Change the Caption
  1. Open the form in Design View.
  2. Select the command button.
  3. Press F4 to open the Property Sheet.
  4. Find the Caption property.
  5. Enter text such as Save Record or Close Form.
  6. Save the form and test the button.

8. Using VBA with a Command Button

For actions that require custom logic, you can use Visual Basic for Applications (VBA). For example, a command button can open a specific form.

Example: Open the Students Form
Private Sub cmdOpenStudents_Click()

    DoCmd.OpenForm "frmStudents"

End Sub

When the button named cmdOpenStudents is clicked, Access opens the form named frmStudents.

Example: Close the Current Form
Private Sub cmdClose_Click()

    DoCmd.Close acForm, Me.Name

End Sub

This code closes the form containing the button.

Example: Open a Report in Print Preview
Private Sub cmdViewReport_Click()

    DoCmd.OpenReport "rptStudentList", acViewPreview

End Sub

Replace the example form and report names with the actual names of objects in your database.

Important: These procedures must be placed in the form's VBA code module and connected to the corresponding button's On Click event. They do not run merely because the code is written on a lesson page.

9. Common Problems and Solutions

Problem Possible Solution
Button does nothing. Check its On Click event, macro, or VBA procedure.
Form does not open. Verify the form name and make sure the form exists.
Report does not open. Verify the report name and its data source.
Wizard does not appear. Enable Use Control Wizards on the Design tab.
Button text is unclear. Change the Caption property to a meaningful label.
VBA reports a missing procedure. Check the event procedure name and the button's On Click setting.

10. Practical Assignment

Create a main menu form for a student management database.

Required Objects
  • frmMainMenu
  • frmStudents
  • frmCourses
  • rptStudentList
Tasks
  1. Create a command button that opens frmStudents.
  2. Create another button that opens frmCourses.
  3. Add a button that opens rptStudentList in Print Preview.
  4. Add a Close button to close the current form.
  5. Change each button's Caption property.
  6. Rename the controls using meaningful names.
  7. Test every button in Form View.
  8. Try implementing one button using VBA.
Goal: Learn to create and configure command buttons for common database navigation tasks.

11. Revision Questions

  1. What is a Command Button in MS Access?
  2. How do you create a command button?
  3. What is the purpose of the Command Button Wizard?
  4. How can a button open another form?
  5. How can a button close the current form?
  6. What is the Caption property?
  7. What is the purpose of the On Click event?
  8. What does DoCmd.OpenForm do?
  9. How can a command button open a report in Print Preview?
  10. What should you check if a command button does nothing?

🧠 Quick Quiz

1. What is the main purpose of a Command Button?




2. Which VBA command opens a form?




3. Which property specifies what happens when a button is clicked?