Today, we continue our discussion of macros and look at how to set macro conditions in the new Access 2010 macro designer.
In some cases, you might want to execute an action or a series of actions in a macro only if a particular condition is true. For example, you can use a macro to validate data in a form. Using conditions, you might want to display one message in response to one set of values and another message in response to a different set of values.
To execute macro actions only when certain conditions are true, you use an If block (this replaces the Condition column in previous versions of Access). When an If block is executed, Access evaluates the condition ñ if it is true, then Access carries out the action associated with the condition. If the condition is false, Access ignores the action and any immediately following actions for that condition.
You can evaluate more than one condition by using the Else if and Else blocks. For instance, if the first condition is false, Access moves to the Else if block and evaluates the condition there. If that condition is false, it then moves to the next Else if block (if any). If all conditions are false, then Access performs the action under the Else block.
In the condition example above, the condition references a control on a form. The syntax might at first appear strange to you although it is quite logical. This construction can also be used to reference controls on a form or on a report from queries, which can be quite useful. The syntax is broken down as follows:
To Use Macro Conditions
- Create a blank new Macro.
- Click the Add New Action drop-down arrow and choose If from the list.
Expand the Program Flow category in the Action Catalog window and double-click If.
- In the Conditional Expression box, type the conditional expression
Use the Expression Builder to create a conditional expression.
- Add the actions you want the macro to carry out if the condition is true.
- To test for more than one condition, click the Add Else If link.
- In the Conditional Expression box in the Else If box, type the conditional expression (or use the Expression builder to create the expression).
- Repeat steps 5 & 6 for any additional conditions you wish to test for.
- Repeat Steps 3 and 4 for any other conditions you want to include in the macro.
- To add an action that is to be executed if all conditions evaluate to false, click the Add Else link and then choose the action to be executed.