Print a Message on a Report if Certain Conditions Are Met

Problem

On a letter that you mail to all the customers on a mailing list, you want to print a message on only some customers’ letters (depending on, for example, the customer’s zip code, credit status, or past orders). How do you make a text box print only when certain conditions are met?

Solution

You can create an event procedure that’s called from the Format event of a report section to make a single control—or an entire section—visible or invisible depending on a condition you specify. This solution shows you how to create a simple event procedure that checks each report record for a certain condition and then prints a message only if that condition is met.

Follow these steps to add an event procedure to your report that prints a message only for certain rows:

  1. Create a new report or open an existing report in design view. Add to the page header section any controls that you wish to show for only selected records. In the rptMailingByZipWithCondition sample, we included three labels and a rectangle control in the page header section.

  2. While the cursor is still located in the page header section, select View Properties to view the section’s properties sheet (if it’s not already open).

  3. Create a new event procedure for the section’s Format event. (If you’re unsure of how to do this, see the Section P.5.5 in the the preface of this book.)

  4. Add to the Format event procedure an If...Then statement with the following basic ...

Get Access Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.