A macro in Microsoft Access is a tool used to automate tasks and perform actions in a database. Macros can open forms, run queries, display messages, apply filters, and respond to events such as clicking a button.
A macro is a collection of actions that Access executes automatically when the macro is run or when an associated event occurs.
Suppose you have a student management database. You want to open the Student Details form whenever you click a button. Instead of performing the action manually each time, you can create a macro to open the form.
A standalone macro is saved as a separate database object. You can run it manually or call it from another macro or an event where appropriate.
Example: A macro named OpenStudentForm.
An embedded macro is stored directly in a form or report event property instead of being saved as a separate named macro object.
Example: An embedded macro attached to the Click event of a command button.
Data macros are associated with table events or named data operations. They can automate actions when records are inserted, updated, or deleted, depending on the event and supported actions.
Follow these steps to create a standalone macro:
OpenStudentForm
The exact options shown in Macro Builder can vary depending on your Access version and database settings.
| Macro Action | Purpose |
|---|---|
| OpenForm | Opens a specified form. |
| OpenReport | Opens a specified report. |
| OpenQuery | Opens a specified query. |
| RunSQL | Executes a supported SQL statement. |
| MessageBox | Displays a message to the user. |
| GoToRecord | Moves to a specified record. |
| ApplyFilter | Applies a filter to display matching records. |
| CloseWindow | Closes a specified window or database object. |
| SetValue | Sets the value of a control or property where supported. |
Suppose your database contains a form named
frmStudents. You want a macro to open it.
OpenForm action.frmStudents.OpenStudentForm.
Access opens the frmStudents form when the
macro runs, provided the form exists and can be opened.
Macro Name: OpenStudentForm
Action: OpenForm
Form Name: frmStudents
frmStudents with the actual form
name used in your database.
You can use the MessageBox action to display
information or instructions to a user.
MessageBox action.Action: MessageBox
Message: Welcome to Student Management System
Title: Welcome
Type: Information
When the macro runs, Access displays a message box containing the configured text.
You can associate a macro with the Click event of a command button on a form.
A button named btnOpenStudents can run a
macro that opens the student form.
Button: btnOpenStudents
Event: On Click
Action: OpenForm
Form Name: frmStudents
The button name is an example. Use the names of your actual form and controls.
Macro conditions let you execute actions only when specified criteria are satisfied. They are useful for controlling database workflows.
Suppose a form contains a control named
txtFee. You want to show a warning when
the fee is zero.
Condition:
[Forms]![frmStudents]![txtFee]=0
Action:
MessageBox
Message:
Please enter the student fee.
This is an illustrative macro condition. The form and control names must match your database, and the condition must be configured using Macro Builder.
| Macros | VBA |
|---|---|
| Actions are configured through Macro Builder. | Automation is written using Visual Basic for Applications code. |
| Suitable for many common database tasks. | Suitable for complex logic and custom procedures. |
| Often easier for beginners to understand. | Requires knowledge of programming syntax. |
| Supports conditions and many built-in actions. | Supports variables, loops, functions, error handling, and custom logic. |
| Some actions are limited by context and security settings. | Code execution can also be affected by security and trust settings. |
| Problem | Possible Solution |
|---|---|
| The macro cannot open a form. | Verify the form name and confirm that the form exists. |
| A button does not run the macro. | Check the button's On Click event property. |
| An action produces an error. | Review the action arguments and required object names. |
| A macro is blocked. | Check database trust and security settings. |
| A condition does not work as expected. | Check the expression, field names, and data types. |
| The macro cannot find a control. | Verify the form name and control name in the expression. |
frmStudents.OpenStudentForm.1. What is the main purpose of a macro in Access?
2. Which macro action is used to open a form?
3. Where can you associate a macro with a command button?