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.
Make sure you have the frmEmployees form from the previous lesson open. Click the View button on the toolbar to switch to Design view.
We want to add a conditional expression to the mcrEmployees macro. If a macro is assigned to a control on a form or report, you can open and edit the macro directly from the form or report without having to access it through the Database window.
Select the command button, click the Properties button on the toolbar, click the Event tab, and click the On Click box.
A Build button appears in every event property. Click this button to create or modify the macro or Visual Basic procedure assigned to the event.
Click the Build button.
The mcrSales macro appears in Design view.
Click the Conditions button on the toolbar.
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.
Click the first blank cell in the Condition column and click the Build button on the toolbar.
The appears, as shown in Figure 10-8.
Double-click the Forms folder in the bottom-left window, double-click the All Forms folder, then click thefrmEmployees folder.
When you select the frmEmployees folder in the left window, the middle window displays all the controls in the selected form.
Scroll down the middle window, and find and double-click the City control.
Access adds Forms![frmEmployees]![City] to the expression area. Now you need to specify how you want to evaluate the City field.
Click in the Expression box and add "Minneapolis".
Your expression should look similar to the one in Figure 10-9.
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.
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."
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.
Next you need to add an action to perform if the City is equal to "Minneapolis."
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.
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.
We're finished modifying the macro.
Save your changes and close the Macro Design window. Click the View button on the toolbar to display the form in Form view.
Let's test our conditional macro.
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.