MS-Access / Getting Started

Testing Your Data Macro

You've now completed all the steps necessary to prevent any new records from being added to the tblWeekDays table. To test the data macro that you've created so far, you first need to save your changes to the Before Change event. Click the Save button in the Close group on the Design contextual tab under Macro Tools or click the Save button on the Quick Access Toolbar. Now click the Close button in the Close group to close the Logic Designer window and return to the Datasheet view of the tblWeekDays table. To test this Before Change event, you need to create a new record in this table. Click in the Week- DayText field on the new record line of the datasheet, enter any text other than one of the existing weekday names, and then tab or click outside of the new record line. Access displays the custom error that you created in the RaiseError data action.

The Before Change event fires because you are inserting a new record into this table. In this event, Access checks to see if a new record is being inserted using the IsInsert property. If the IsInsert property is True, the RaiseError data action fires, Access displays the custom message that you created, and then Access cancels the insert. When you click OK in the message box, Access keeps your focus on the new record that you are trying to insert. You now need to press Esc to undo your changes to the new record. There is no way that you can add records to this table unless you remove the data macro that you defined in the Before Change event of the table. Access enforces this restriction no matter what the entry point is for creating a new record. As you can see, data macros are a very powerful new feature in Access 2010.

[Previous] [Contents] [Next]