working with macros chapter 10 working with macros

28
CHAPTER 10 WORKING WITH MACROS

Upload: homer-chapman

Post on 24-Dec-2015

293 views

Category:

Documents


7 download

TRANSCRIPT

Page 1: WORKING WITH MACROS CHAPTER 10 WORKING WITH MACROS

CHAPTER 10WORKING WITH

MACROS

Page 2: WORKING WITH MACROS CHAPTER 10 WORKING WITH MACROS

1. Introduction

A macro helps you perform routine tasks by automating them

Record a single macro that does the entire task all at once instead of manually performing a series of time-consuming, repetitive actions.

A macro is a set of one or more actions that perform a particular operation

Page 3: WORKING WITH MACROS CHAPTER 10 WORKING WITH MACROS

2. Creating and Running a MacroSimple macros that automate a single

task, such as opening a form or report, are simple to create

Complicated macros with several steps or procedures require planing.

Before creating a complicated macro, identify what the macro will do and the individual actions that are required to complete this operation.

Page 4: WORKING WITH MACROS CHAPTER 10 WORKING WITH MACROS

2. Creating and Running a MacroIn the database window, click the

macros icon in the objects bar and click the new button.

In action column, select the action you want the macro to perform.

Specify any required arguments for the action in the action arguments area.

Cick the save button on the toolbar

Page 5: WORKING WITH MACROS CHAPTER 10 WORKING WITH MACROS

2. Creating and Running a Macro

Page 6: WORKING WITH MACROS CHAPTER 10 WORKING WITH MACROS

2. Creating and Running a MacroClick the macros icon in the objects bar

and double-click the macro you want to run or click run button

Page 7: WORKING WITH MACROS CHAPTER 10 WORKING WITH MACROS

3. Editing a Macro

Macros can contain many actions to automate even the most complicated tasks

Each action appears in its own row and is executed in the order in which it appears in the Macro window, from top to bottom.

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.

Page 8: WORKING WITH MACROS CHAPTER 10 WORKING WITH MACROS

4. Working with Macro GroupsA macro group stores several related

macros together in a single macro objectTo create a macro group, give each

macro in the macro group its own unique name by entering the macro names in the Macro Name column (by clicking the Macro Names button on the toolbar).

When combine two or more macros within the same macro group, must run them separately, by referring to the macro group name, followed by the macro name

Page 9: WORKING WITH MACROS CHAPTER 10 WORKING WITH MACROS

4. Working with Macro Groups

Page 10: WORKING WITH MACROS CHAPTER 10 WORKING WITH MACROS

4. Working with Macro GroupsTo run a macro in a macro group:Select tools macro run macro from the

menu.Click the macro name list arrow, select

the macro you want to run, and click ok

Page 11: WORKING WITH MACROS CHAPTER 10 WORKING WITH MACROS

5. Assigning a Macro to an Event

To assign a macro to a control on a form or report:

• Open the form or report in design view.• Click the control to which you want to

assign the macro and click the properties button on the toolbar.

Page 12: WORKING WITH MACROS CHAPTER 10 WORKING WITH MACROS

5. Assigning a Macro to an Event• Click the event tab and click in the box

for the type of event you want to assign to the macro.

• Select the macro you want to assign to the event.

• Close the properties dialog box and save the form or report.

Page 13: WORKING WITH MACROS CHAPTER 10 WORKING WITH MACROS

5. Assigning a Macro to an Event

Event Description

Before Update

Macro or function that runs when data in a field or record is changed but before the changes are actually saved to the database. Often used to validate data.

After Update

Macro or function that runs when data in a field or record is changed and is saved to the database.

Page 14: WORKING WITH MACROS CHAPTER 10 WORKING WITH MACROS

5. Assigning a Macro to an Event

On Change

Macro or function that runs when the contents of a text box or combo box changes or when you move from one page to another page in a tab control.

On Enter

Macro or function that runs when a control first gets the focus. The Enter event occurs before the focus moves to a particular control. You can use an Enter macro or event procedure to display instructions when a form or report first opens.

On ExitMacro or function that runs when a control loses focus (is deselected) on the same form.

Page 15: WORKING WITH MACROS CHAPTER 10 WORKING WITH MACROS

5. Assigning a Macro to an Event

On Got Focus

Macro or function that runs when a control gets the focus (is selected).

On Lost Focus

Macro or function that runs when a control loses the focus (is deselected).

On ClickMacro or function that runs when a control is clicked.

On Dbl Click

Macro or function that runs when a control is double-clicked.

Page 16: WORKING WITH MACROS CHAPTER 10 WORKING WITH MACROS

5. Assigning a Macro to an EventOn Mouse Down

Macro or function that runs when the user presses the mouse button.

On Mouse Move

Macro or function that runs when the user moves the mouse over a control.

On Mouse Up

Macro or function that runs when the user releases the mouse button.

On Key Down

Macro or function that runs when the user presses a key on the keyboard.

On Key UpMacro or function that runs when the user releases a key on the keyboard.

On Key Press

Macro or function that runs when the user presses an ANSI key on the keyboard.

Page 17: WORKING WITH MACROS CHAPTER 10 WORKING WITH MACROS

6. Creating Conditional ExpressionsThe macro condition states this

effectively: If this condition is true, run this action. If it is not true, go to the next action, if any.

Normally, a condition applies only to the action on the same row in the macro sheet. If the condition isn’t met, the next action is executed.

To continue the condition to the next action, enter an ellipsis (…) in the Condition column of the next row. You can apply the condition to several sequential actions

Page 18: WORKING WITH MACROS CHAPTER 10 WORKING WITH MACROS

6. Creating Conditional Expressions

Page 19: WORKING WITH MACROS CHAPTER 10 WORKING WITH MACROS

6. Creating Conditional ExpressionsTo add the Condition column to the

macro sheet, click the Conditions button or choose View | Conditions.

Type the logical expression for the condition in the row with the action you want to carry out if the condition is True.

If you want to use the Expression Builder to help with the expression, right-click in the Condition column and choose Build from the shortcut menu.

Page 20: WORKING WITH MACROS CHAPTER 10 WORKING WITH MACROS

7. Some Common Uses for MacrosDisplaying a Message Box

MsgBoxdisplay warnings, alerts, and other information

Message Enter the text of the message you want displayed when the

Beep sound a beep signal when the message

Type Sets the type of message box, each of which displays a

Title Text that displays in the message box title bar

Page 21: WORKING WITH MACROS CHAPTER 10 WORKING WITH MACROS

7. Some Common Uses for MacrosSetting Values and Properties

◦Set the value of a control based on the value of another control in the same or a different form or report

Many of the properties of forms, reports, and controls can be set by running a macro. For example, you can hide a control from view on the form or disable it so the user can’t enter data in it.

Page 22: WORKING WITH MACROS CHAPTER 10 WORKING WITH MACROS

7. Some Common Uses for MacrosThe MsgBox function is similar to the MsgBox

action with the exception that the function returns one of seven different values, depending on which button the user clicks in the message box

The MsgBox function displays a dialog box containing the message and waits for the user to click a button indicating the user’s choice

The MsgBox function has three main arguments; only the first is required

Page 23: WORKING WITH MACROS CHAPTER 10 WORKING WITH MACROS

7. Some Common Uses for Macros

◦Prompt is a string expression displayed in the dialog box.

◦Button is a number equal to the sum of three values that specify the visual characteristics of the message box

◦Title is a string expression displayed in the dialog box title bar

Page 24: WORKING WITH MACROS CHAPTER 10 WORKING WITH MACROS

7. Some Common Uses for MacrosYou can also specify which of the buttons

is the defaultYou can also specify which of the buttons is the default

Value Buttons To Display:0 Display only the OK button.1 Display the OK and Cancel buttons.2 Display the Abort, Retry, and Ignore buttons.3 Display the Yes, No, and Cancel buttons.4 Display the Yes and No buttons.5 Display the Retry and Cancel buttons.

Page 25: WORKING WITH MACROS CHAPTER 10 WORKING WITH MACROS

7. Some Common Uses for Macros

Icons To Display:

0 Display no icon.

16 Display the Critical Message icon.

32 Display the Warning Query icon.

48 Display the Warning Message icon.

64 Display the Information Message icon.

Page 26: WORKING WITH MACROS CHAPTER 10 WORKING WITH MACROS

7. Some Common Uses for MacrosWhen you use the MsgBox function in a

macro condition, you can compare the returned value to a specific number and carry out the action if the comparison is True Button Returned Value

OK 1Cancel 2Abort 3Retry 4Ignore 5Yes 6No 7

Page 27: WORKING WITH MACROS CHAPTER 10 WORKING WITH MACROS

8.Creating an AutoExec MacroYou can create a special macro that runs

when you first open a database. The AutoExec macro can carry out such actions as open a form for data entry, display a message box.

All you need to do is create the macro with the actions you want carried out at startup and save it with the name AutoExec. Adatabase can have only one macro named AutoExec

Page 28: WORKING WITH MACROS CHAPTER 10 WORKING WITH MACROS

9. Macro Action Reference

(tự tham khảo)