MS-Access / Getting Started

Moving Actions

As you design data macros or user interface macros in the new Logic Designer, you might find that you need to move actions around as the needs of your application change. In the Before Change event for the tblWeekDays table, you created a data macro that prevents new records from being added to the table and existing records from being changed. We had you create two separate Groups and If blocks to accomplish this goal. As with many areas of Access, there is usually more than one way to accomplish a task. If you analyze the logic we created in the Before Change event, you'll realize we could simplify the logic further by using only one If block instead of two. The Logic Designer makes the task of moving actions around the macro design surface very easy.

Open the tblWeekDays table in Datasheet view if you closed it, click the Table contextual ribbon tab under Table Tools, and then click the Before Change button in the Before Events group. You should now see the data macro that you created previously for preventing new records from being added to this table and changing existing values. The first If block tests to see if a new record is being added to the table by using the IsInsert property. The second If block tests to see if the data in the WeekDayText field is being changed. We can simplify this data macro further because if the IsInsert property is False, the user must be trying to change existing data. (Remember that the Before Change event fires only when adding new records or changing data in existing records.) The tblWeekDays table has only two fields- WeekDayID and WeekDayText. WeekDayID is the ID field for the table and is therefore read-only. As a result, we can simply add an Else block to the first If block and not include the conditional test using the Updated function in the first test. To make these changes, you first need to add an Else block to the first If block. Click inside the first If block and then click the Add Else link at the lower-right corner of the If block.

Access creates an Else block below the RaiseError data action. Access also places an Add New Action combo box inside this Else block. When Access runs this data macro on the Before Change event, it first checks the conditional expression in the If statement. If the IsInsert property is False, Access moves down to the Else block and then executes any actions inside the Else block. Note that if you added an Else If block instead of an Else block, you would need to provide an additional conditional expression for Access to execute actions inside the Else If block.

In previous sections, you've already created an appropriate RaiseError data action to handle the condition when a user is trying to change existing data. You could recreate the entire RaiseError data action inside the Else block, but it's much quicker to move the existing RaiseError inside this Else block. To move the RaiseError data action that traps for data changes, click anywhere on the RaiseError data action, hold the mouse key down, drag the action block up to the Else block until the Else keyword is highlighted, and then release the mouse.

Access places the RaiseError data action inside the Else block. Instead of using the drag technique, you could also click the Up arrow button on the far side of the RaiseError to move it up into the Else block. When you click the Up arrow button, Access moves the selected action up one position in the macro design surface. In our example, it would take five clicks of the Up arrow to move the second RaiseError data action up into the Else block.

To duplicate any logic on the macro design surface, you can hold the Ctrl key down and then drag to a different location. Access creates an exact copy of the program flow construct, data block, or data action, including any argument information.

You might find it easier to use the keyboard rather than the mouse to move actions around the macro design surface. Table-2 lists the keyboard shortcuts for working inside the Logic Designer.

Table-2 Keyboard Shortcuts for Logic Designer
Keys 		Action
Ctrl+F 		Moves focus to the Search box in the Action Catalog
Ctrl+F2 	Opens the Expression Builder dialog box if in an expression context
Ctrl+Space 	Calls up IntelliSense in expression contexts
Ctrl+Up arrow 	Moves selected action up
Ctrl+Down arrow Moves selected action down
Shift+F2 	Opens the Zoom Builder dialog box
Shift+F10 	Opens a context-sensitive shortcut menu
Left arrow 	Collapse action
Right arrow 	Expand action

Now that you've moved the RaiseError data action out of the second Group block in this data macro, we no longer need that Group block or the If block inside it. Highlight the second Group block, called PreventDataChanges, and then click the Delete button on the right side of the block to remove it from the macro design surface.

We still have the extra Comment block at the bottom of the macro design surface, which we really don't need now that we removed the second Group block. However, we should probably add additional information into the first Comment block to mention that the data macro prevents any existing data from being changed. Click into the first Comment block, move your cursor to the end of the text, and then type the following additional text:

We also prevent any data from being changed in the Else block by raising an error in that condition as well.

Now, highlight the Comment block at the bottom of the macro design surface and then click the Delete button on the far right to remove it. Finally, click in the Group block and rename it from PreventNewRecords to PreventNewRecordsOrDataChanges. Your revised Before Change event for the tblWeekDays table.

You've now successfully revised the data logic in the If conditional block, moved the second RaiseError data action, and condensed the data macro. Even with the changes you made, the data macro in this Before Change event still functions just as it did before. Save your changes, close the Logic Designer window, and test it by trying to add new records to the table and edit existing data. You'll see that Access prevents you from making these changes, just as it did before you made the design adjustments.

[Previous] [Contents] [Next]