Validate data programmatically

Microsoft Office Access 2003

  1. To create a macro, click Macros Button image under Objects.
  2. Click the New button on the Database window toolbar.
  3. Add an action to the macro.

    ShowHow?

    1. In the Macro window, click the first empty row in the Action column. If you want to insert an action between two action rows, click the selector for the action row just below the row where you want to insert the new action, and then click Insert Row Button image on the toolbar.
    2. In the Action column, click the arrow to display the action list.
    3. Click the action you want to use.
    4. In the lower part of the window, specify arguments for the action, if any are required. For action arguments whose settings are a database object name, you can set the argument by dragging the object from the Database window to the action's Object Name argument box.
    5. Type a comment for the action. Comments are optional.
  4. In the Macro window, make sure Conditions Button image on the toolbar is pressed in.
  5. In the Condition column of a blank action row, enter a validation condition.
  6. In the Action column, click the action in the action list that you want to carry out if the validation condition is true.
  7. For any additional actions you want to carry out if the validation condition is true, enter an ellipsis (...) in the Condition column followed by the action in the Action column. For example, it's usually a good idea to carry out a CancelEvent action for each validation condition after any other actions have been carried out.
  8. Click Save Button image to save the macro.
  9. Open the form in Design view.
  10. For the event that you want to trigger the validation, set the event property to the name of the macro. For example, to trigger the validation when a control is updated with changed data, set the BeforeUpdate event property of the control to the name of the macro.

    Note  To validate data before a record as a whole is updated, attach the validation macro to the form's BeforeUpdate event rather than to the control's BeforeUpdate event.

ShowUse an event procedure

  1. Open the form in Design view.
  2. Display the property sheet for the form and then click the Event tab.

  3. Click the event property for the event that you want to trigger the procedure.

    For example, to trigger the validation rule when a control is updated with changed data, create an event procedure for the control's BeforeUpdate event.

  4. Click Build Button image next to the property box to display the Choose Builder dialog box.

  5. Double-click Code Builder to display the event procedure window.

  6. Add Microsoft Visual Basic for Applications code to the event procedure, specifying the validation condition and the operations to perform when those conditions are met.

Notes

  • It's usually a good idea to cancel the event that triggers the validation after the operations have been performed. You can cancel many events by setting the event procedure's Cancel argument to True.
  • To validate data before the record as a whole is updated, add code to the BeforeUpdate event procedure for the form, rather than for the control.