Professional Documents
Culture Documents
Prerequisites
If you find yourself doing the same routine task over and over again, you might
want to consider creating a macro to complete the task for you. A macro helps
you perform routine tasks by automating them. Instead of manually performing
a series of time-consuming, repetitive actions, you can record a single macro
that does the entire task all at once for you. For example, instead of clicking the
Reports icon in the Objects bar in the database window, finding and opening a
specific report, printing it, and then closing it, you could create a macro to print
the report with the click of a single button.
3971401.doc - 146 -
Creating and Running a Macro
Figure 10-1. You create macros
In some programs, such as Microsoft Excel or Word, you can create macros with
a "macro recorder" to record your commands, keystrokes, and mouse clicks.
Unfortunately, there isn't a "macro recorder" or Macro Wizard to help you create
a macro in Microsoft Access. Instead, you create macros by entering the actions
and arguments directly in Macro Design view, shown in Figure 10-1. Don't
worry it's not as difficult as it sounds. Working in Macro Design view really isn't
all that different from working in Table Design view it's where you define and
edit your macro objects.
Simple macros that automate a single task, such as opening a form or report,
are incredibly simple to create we'll create such a macro in this lesson. More
complicated macros with several steps or procedures may require a little bit of
planning. Before you create a complicated macro, think about what you want
the macro to do and the individual actions that are required to complete this
operation. Practice the steps needed to carry out the operation and write them
down as you go it will make writing the macro a lot easier.
And so, without any further ado, let's create our first macro.
1. Start Microsoft Access, open the Lesson 10 database, click the Macros
icon in the Objects bar, and click the New button.
The Macro1: Macro window appears, as shown in Figure 10-1. The Action
cell is where you tell Access what you want the macro to do.
3971401.doc - 147 -
2. Click the first blank Action cell, then click the list arrow.
The OpenForm action is added to the first line of the macro window. Most
of the time you will have to give Access more information about how to
execute each action. For example, here we will have to tell Access which
form to open with the OpenForm action. You use arguments to supply
Microsoft Access with information about how to carry out the action. Each
type of action has its own set of arguments, which appear in the Action
Arguments panel, located at the bottom of the macro window.
4. Click the Form Name text box in the Action Arguments panel, click the list
arrow, and select frmEmployees.
That's the only argument we need to specify for this exercise, but notice
that there are additional arguments for the OpenForm action, such as the
View argument, which lets you select the view in which to open the form
(Form view, Design view, or Print Preview).
If you want, you can type a comment to explain the action in the
Comment column. If you've ever had any programming experience, the
Comment column is the same as a remark statement.
5. Click the first blank Comment cell and type This macro opens the
frmEmployees Form.
Comments are completely optional, but they do make your macros easier
to understand, especially if other users will edit them.
6. Click the Save button on the toolbar, save the macro as mcrEmployees and
click OK.
3971401.doc - 148 -
You're finished working in the Macro window for now so...
Time to test your new macro. The Macros icon in the Objects bar in the
Database window should be selected.
Access runs the mcrEmployees macro and opens the frmEmployees form.
That's all there is to a macronot nearly as hard as you thought it would be, was
it? Even the Microsoft Access developers don't have all the macro actions
memorized, so make sure you have Table 10-3 handy so that you know what
action to use in your macros.
3971401.doc - 149 -
Reference
TO CREATE A MACRO:
TO RUN A MACRO:
Editing a Macro
Figure 10-2. The mcrEmployees macro with two
additional actions.
3971401.doc - 150 -
Figure 10-3. A message boxthe result of the MsgBox action.
Some Microsoft Access tasks require several steps. For example, a particular
task might require you to (1) open a form, (2) select a specific record, (3)
select a specific field in that record, and then (4) copy the information in that
field to the Windows clipboard. Macros can contain as many actions as
necessary to automate even the most complicated tasks. Each action appears in
its own row and is evaluated and executed in the order in which it appears in
the Macro window, from top to bottom.
In this lesson you will edit the macro you created in the previous lesson to
change its arguments and add some more steps or actions.
The mcrEmployees macro opens in Design view. Let's add two more
actions to this macro.
2. Click the Action cell just below the OpenForm action, click the list arrow,
scroll down the list, and select Maximize.
When you run the macro, the Maximize action will maximize the window
so that it fills the entire Microsoft Access window. Because the Maximize
action is so simple and straightforward, it doesn't have any additional
arguments.
3. Click the Action cell just below the Maximize action, click the list arrow,
scroll down, and select MsgBox.
When you run the macro, the MsgBox action will display a message box
that contains a warning or an informational message. The Message
argument is the most important argument for the MsgBox action because
it determines the text that is displayed in the message box.
3971401.doc - 151 -
4. Click the Message argument box and type Please make sure that you
remember to add the employee's phone number!.
There are several other arguments you may want to specify for the
MsgBox action, such as the type of icon that is displayed in the message
box (None, Critical, Warning?, Warning!, or Information) and the text that
is displayed in the title bar of the message box.
5. Click the Type argument box, click the list arrow, and select Information.
Click the Title argument box and type Notice.
Your macro should look like the one shown in Figure 10-2. Remember that
actions are evaluated and/or executed in the order in which they appear,
so the mcrEmployees macro will (1) open the frmEmployees form, (2)
maximize the form window, and (3) display the message box.
9. Click OK to close the message box, then click the Close button to close
the frmEmployees form.
3971401.doc - 152 -
Reference
TO MODIFY A MACRO:
If you are creating lots of macros, you might want to consider organizing them
into a macro group to help you manage them. A macro group stores several
related macros together in a single macro object. When you create a macro
group, you must give each macro in the macro group its own unique name to
3971401.doc - 153 -
identify where each macro starts and ends. You do this by entering the macro
names in the Macro Name column, which you display by clicking the Macro
Names button on the toolbar.
When you combine two or more macros within the same macro group, you
must run them separately, by referring to the macro group name, followed by
the macro name. For example, mcrEmployees. mcrEmployees frmEmployees
refers to the Open frmEmployees macro in the mcrEmployees macro group.
In this lesson you will learn how to group several related macros together in a
macro group.
In order to work with macro , you need to display the Macro Name
column.
First you need to give the macro you created in the previous two lessons
a name. The cursor is already positioned in the Macro Name cell of the
first row.
Note: Always enter the macro name in the Macro Name column, next to
the Action where the macro starts.
3. Type Open frmEmployees in the first blank cell in the Macro Name column.
The macro name "Open frmEmployees" identifies the macro you created
in the previous two lessons. To create another macro in the same macro
group, type its name in the Macro Name column next to the first action of
the new macro.
The cursor should be positioned in the Macro Name column next to the
first blank Action row. This is where you will add another macro to the
macro group.
3971401.doc - 154 -
5. Type Print rptEmployeeSales.
6. Click the Action cell to the right of the Print rptEmployeeSales macro
name, click the list arrow, scroll down, and select OpenReport.
7. Click the Report Name text box in the Action Arguments panel, click the
list arrow, and select rptEmployeeSales.
This macro will open the rptEmployeeSales report. Notice Print appears in
the View argument boxthis will send the rptEmployeeSales report directly
to the printer. Let's add a comment to this new macro.
8. Click the blank Comment box in the Print rptEmployeeSales macro row
and type This macro prints the rptEmployeeSales report.
You want to add one more action to the Print rptEmployeeSales macro.
9. Click the Action cell just below the OpenReport action, click the list arrow,
scroll down, and select MsgBox.
You need to tell Access what you want the message box to say.
10. Click the Message argument box and type The Employee Sales report has
been sent to the printer.
Let's specify several additional arguments for the MsgBox action, such as
the type of icon that is displayed in the message box.
11. Click the Type argument box, click the list arrow, and select Information.
Click the Title argument box and type Notice.
Your macro should look like the one shown in Figure 10-4.
12.Click the Save button on the toolbar and close the macro window.
3971401.doc - 155 -
Let's test our new macro. When you combine several macros within the
same macro group you must run them separately using the Tools
command on the Access menu.
The Run Macro dialog box appears, as shown in Figure 10-5. Here's where
you select the specific macro you want to run.
15.Click OK to close the message box and close the frmEmployees form.
If you want, go ahead and repeat Step 13 and run the Print rptEmployeeSales
macro. Make sure your computer is connected to a printer, as this macro will
send a copy of the rptEmployeeSales report to the printer.
3971401.doc - 156 -
Reference
3971401.doc - 157 -
Running macros from the Database window or menu is a pain in the neck.
That's why most database developers assign macros to controlsparticularly,
buttonsso that when a user clicks the button or control, a macro is activated.
2. Click the Toolbox button, if necessary, and click the Command Button
button on the Toolbox.
The mouse pointer changes to a , indicating that you can click and drag
the command button control onto the form.
3. Place the pointer below the DOB field label and click and drag the
pointer down and to the right to create a command button like the one in
Figure 10-6. Click Cancel if the Command Button Wizard appears.
4. Make sure that the command button is still selected, then click its text
label and replace the text with Sales Report. Click anywhere outside of the
command button when you're finished.
We're ready to assign a macro to the buttonto do this you will need to
display the command button's Properties.
5. Select the command button, click the Properties button on the toolbar,
and click the Event tab.
3971401.doc - 158 -
The lists all the events to which you can assign a macromost of them you
will never use, as you can see in Table 10-1.
6. Click the On Click box, click the list arrow, and select mcrSales, as shown
in Figure 10-7. Close the Properties dialog box when you're finished.
7. Click the View button on the toolbar to switch to Form view, then click the
new Sales Report button.
Microsoft Access runs the mcrSales macro and displays the Employee
Commission Report for the current employee. Let's close the report and
save our changes...
8. Close the Employee Commission Report and then click the Save button.
After Macro or function that runs when data in a field or record is changed
Update and is saved to the database.
Macro or function that runs when the contents of a text box or combo
On
box changes or when you move from one page to another page in a
Change
tab control.
Macro or function that runs when a control first gets the focus (is
selected). The Enter event occurs before the focus moves to a
On Enter particular control (before the GotFocus event). You can use an Enter
macro or event procedure to display instructions when a form or
report first opens.
Macro or function that runs when a control loses focus (is deselected)
On Exit
on the same form.
On Got Macro or function that runs when a control gets the focus (is
Focus selected).
On Lost Macro or function that runs when a control loses the focus (is
3971401.doc - 159 -
Event Description
Focus deselected).
On Macro or function that runs when the user presses the mouse button.
Mouse
Down
On Macro or function that runs when the user moves the mouse over a
Mouse control.
Move
On Macro or function that runs when the user releases the mouse
Mouse button.
Up
On Key Macro or function that runs when the user presses a key on the
Down keyboard.
On Key Macro or function that runs when the user releases a key on the
Up keyboard.
On Key Macro or function that runs when the user presses an ANSI key on
Press the keyboard.
3971401.doc - 160 -
Reference
3971401.doc - 161 -
Figure 10-10. The updated mcrSales macro.
You enter conditions in the Condition column in the Macro window. If a condition
is true, Access executes the action in that row. If a condition isn't true, Access
skips the action in that row and moves to the next row. Conditions often
compare values in a specific control on a form or report to a number, date, or
constant. For example, the expression in Figure 10-9 evaluates if the value in a
City field is not equal to "Minneapolis." Make sure that you use the proper
Microsoft Access syntax when referring to controls in forms or reports.
1. Make sure you have the frmEmployees form from the previous lesson
open. Click the View button on the toolbar to switch to Design view.
2. Select the command button, click the Properties button on the toolbar,
click the Event tab, and click the On Click box.
3971401.doc - 162 -
3. Click the Build button.
The Condition column appears. This is where you need to add the
conditions you want Access to evaluate before it executes an action. It's
often easier if you use the Expression Builder to help you create your
macro conditions.
5. Click the first blank cell in the Condition column and click the Build button
on the toolbar.
When you select the frmEmployees folder in the left window, the middle
window displays all the controls in the selected form.
7. Scroll down the middle window, and find and double-click the City control.
9. Click OK.
The Expression Builder dialog box closes. The condition you entered will
execute the OpenForm action only if the City field is not equal to
"Minneapolis." The condition you entered only affects the first row or
action in the macrothe other actions in the macro will execute without
being evaluated. If you want to evaluate the other actions, they must
each have their own statement in the Condition column. Let's add some
more actions to the macro.
3971401.doc - 163 -
10.Copy the first row in the Condition column and paste it in the second and
third rows.
Add another action that will execute only if the City is not equal to
"Minneapolis."
11. Click the Action cell in the second row, click the list arrow and select
MsgBox. Click the Message argument box and type This is the current
commission for non-Minneapolis employees.
12. Edit the expression in the third row of theCondition column so it reads
[Forms]![frmEmployees]![City]="Minneapolis".
Your macro should look like Figure 10-10. Now you need to specify the
action to perform if the condition is true.
13. Click the Action cell next to the condition you edited, click the list arrow,
and select MsgBox. Click the Message argument box and type Call Linda
Ross for the Minneapolis Commission report.
14.Save your changes and close the Macro Design window. Click the View
button on the toolbar to display the form in Form view.
15.Find a record whose City field is NOT "Minneapolis" and click the Sales
Report button. Click OK and then close the commission report. Click the
Save button and close the frmEmployees form.
3971401.doc - 164 -
Reference
3971401.doc - 165 -
Assigning a keystroke combination to macro can be a somewhat complicated
process. There are two things you need to know about assigning a macro to a
keystroke combination:
• You must create a special macro group, named , which contains all your
keystroke-combination macros.
• You type the keystroke combination to which you want to assign the
macro in the Macro Name column of the AutoKeys macro window. Enter
the keystroke combinations using the examples in Table 10-2. For
example, to assign a macro to the keystroke combination Ctrl + D, you
would name the macro ^D.
In this lesson you will learn how to create an AutoKeys macro to assign a macro
to a keystroke combination.
1. In the Database window, click the Macros icon in the Objects bar and then
click the New button.
The Macro window appears. You need to make sure the Macro Name
column is displayed in order to tell Access which keystroke combination
you want to assign to the macro.
Now you need to type the keystroke combination to which you want to
assign the macro. Table 10-2 shows the key combinations you can use to
make key assignments in an AutoKeys macro group. We want to assign a
macro to the keystroke combination Ctrl + L. Here's what you need to
enter:
3. Type ^L in the first blank Macro Name cell, as shown in Figure 10-11.
The name ^L refers to Ctrl + L. The ^ (caret) signifies the Ctrl key and
the "L" signifies the "L" key.
3971401.doc - 166 -
keystroke combination will override the Microsoft Access keystroke
combination assignment.
4. Click the first blank Action cell, click the list arrow, and select RunApp
from the list.
Now let's save the macro, making sure that we name it AutoKeys.
6. Click the Save button on the toolbar, type AutoKeys, and click OK.
7. Press Ctrl + L.
Give yourself a pat on the back if you've gotten through this tutorial. You've just
learned how to automate your Microsoft Access databases to work more like a
full-featured applicationinstead of a dull, static database.
Enter {ENTER}
3971401.doc - 167 -
Heading Heading
Esc {ESC}
Tab {TAB}
Reference
3971401.doc - 168 -
Action Description
FindNext Finds the next record that meets the criteria specified with
the most recent FindRecord action or Find dialog box. Use
to move successively through records that meet the same
criteria.
3971401.doc - 169 -
Action Description
3971401.doc - 170 -
Action Description
Review
Lesson Summary
3971401.doc - 171 -
• To Create a Macro: In the database window, click the Macros icon in the
Objects bar and click the New button. Click the first blank Action cell, click
the list arrow, and select the action you want the macro to perform.
Specify any required arguments for the action in the Action Arguments
area. Repeat for each additional action you want the macro to execute.
Click the Save button on the toolbar, give your new macro a name, and
click OK.
• To Run a Macro: Click the Macros icon in the Objects bar and double-click
the macro you want to run.
Editing a Macro
• To Modify a Macro: In the Database window, click the Macros icon in the
Objects bar, select the macro you want to edit, and click the Design
button.
3971401.doc - 172 -
toolbar and click the Condition cell next to the action you want to
evaluate. Enter the conditional expression in the Condition cell, using
proper Access syntax. You can use the Expression Builder to help you
create the expression by clicking the Build button on the toolbar. Repeat
these steps for each action you want to evaluate. Save the macro and
close the macro window.
Quiz
1. The fastest and easiest way to create a macro in Microsoft Access is with
the Macro Recorder. (True or False?)
2. A(n) ___, or command, is the basic building block of a macro.
A. Expression.
B. Action.
C. Procedure.
D. Function.
3. Macros may contain more than one action to perform several steps in
sequence. (True or False?)
4. Which of the following statements is NOT true?
A. You can add an optional comment to a macro action in the
Comment column.
B. You use arguments to supply Microsoft Access with information
about how to carry out specific actions.
C. A macro group stores several named macros together in a single
group.
D. To run a specific macro in a macro group, double-click the macro,
select the specific macro from the list, and click Run.
5. Which of the following columns cannot be found in the Macro Design
window?
A. Macro Name column (if the Macro Names button on the toolbar is
selected).
3971401.doc - 173 -
B. Action Arguments column.
C. Condition column (if the Conditions button on the toolbar is
selected).
D. Action column.
6. You can assign a macro to a button, so that when a user clicks the button
or control, a macro is activated. The procedure for doing this is:
A. (1) Open the form in Design View, (2) select the button and display
its properties, (3) click the Event tab and click the On Click box, (4)
click the down arrow and select the macro you want to assign to the
button.
B. (1) Open the macro in Design View, (2) select the form and button
you want to assign the macro to from the Assign box, (3) click
Event box, click the down arrow and select On Click from the list.
C. (1) Open the form in Design View, (2) select the button, and (3)
select the macro from the Macro List on the toolbar.
D. Too lengthy and complicated to be described in a brief quiz
question.
7. Conditions only effect the corresponding Action row in the macrothe other
actions in the macro will execute without being evaluated. If you want to
evaluate the other actions, they must each have their own statement in
the Condition column. (True or False?)
Homework
3971401.doc - 174 -
8. Close the Homework database.
Quiz Answers
1. False. Microsoft Excel and Word both have macro recorders you can use to
create macros, but with Microsoft Access, you create macros in the Macro
Design window.
2. B. An action is the basic building block of a macro.
3. True. Macros can contain multiple actions to automate several steps in a
sequence.
4. D. Double-clicking a macro group will run every named macro in the
group, often with disastrous results.
5. B. This was a trick question. Macro actions do have arguments; however,
they can be found in the Action Arguments panelnot in a column.
6. A. The correct procedure for assigning a macro to a button is: (1) Open
the form in Design View, (2) select the button and display its properties,
(3) click the Event tab and click the On Click box, (4) click the down arrow
and select the macro you want to assign to the button.
7. True. Conditions only effect the corresponding Action row in the macro. If
you want to evaluate the other actions, they must each have their own
statement in the Condition column.
3971401.doc - 175 -