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.
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.
Follow these steps to create a command button:
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 |
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.
Suppose your database contains these forms:
frmMainMenufrmStudentsfrmCoursesfrmMainMenu in Design View.frmStudents.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.
A Close button closes the current form. It is commonly placed on admission forms, data-entry forms, and database navigation menus.
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.
A command button can open a report so users can view summaries such as student lists, fee details, or course information.
rptStudentListrptFeeDetailsrptCourseSummaryYou can configure a separate button for printing when that action is available in the wizard.
You can customize a command button to make its purpose clear to users.
| 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. |
For actions that require custom logic, you can use Visual Basic for Applications (VBA). For example, a command button can open a specific form.
Private Sub cmdOpenStudents_Click()
DoCmd.OpenForm "frmStudents"
End Sub
When the button named cmdOpenStudents
is clicked, Access opens the form named
frmStudents.
Private Sub cmdClose_Click()
DoCmd.Close acForm, Me.Name
End Sub
This code closes the form containing the button.
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.
| 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. |
Create a main menu form for a student management database.
frmMainMenufrmStudentsfrmCoursesrptStudentListfrmStudents.frmCourses.rptStudentList in Print Preview.DoCmd.OpenForm do?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?