Access Cookbook - Ken Getz [62]
3.4. 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:
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.
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).
Create a new event procedure for the section's Format event. (If you're unsure of how to do this, see the How Do I Create an Event Procedure? in the the preface of this book.)
Add to the Format event procedure an If...Then statement with the following basic structure:
If (some condition) Then
Me.Section(acPageHeader).Visible = True
Else
Me.Section(acPageHeader).Visible = False
End If
For example, in rptMailingByZipWithCondition, we added an event procedure that tests if the first two characters of the Zip Code field are equal to 98. The complete event procedure is shown here:
Private Sub PageHeader0_Format(Cancel As Integer, _
FormatCount As Integer)
' Set the visibility of the page header section,
' depending on whether or not the current
' zip code starts with "98".
If Left(Me.ZipPostalCode, 2) = "98" Then
Me.Section(acPageHeader).Visible = True
Else
Me.Section(acPageHeader).Visible = False
End If
End Sub
Save the report and preview it to see if the event procedure is working properly.
Load the rptMailingByZip report from 03-04.MDB. This sample report, which is bound to the tblCompanyAddresses table, is used to print a letter to customers who are sorted by zip code. It includes a message in the page header that announces the company's booth in an upcoming conference. The message prints for all customers, even those outside the Seattle area. Now load rptMailingByZipWithCondition to see an example of a report that selectively prints a message. Notice that this version of the report prints the message only for customers whose zip codes begin with 98 (see Figure 3-8 and Figure 3-9).
Figure 3-8. An address whose zip code does not start with 98, with no message
Figure 3-9. An address whose zip code starts with 98, with the message
Discussion
The event procedure uses the report's Section property and the section's Visible property to make an entire section visible or invisible when the report is formatted. Whether the section is visible depends on its meeting the condition in the If...Then expression. In our example, only zip codes starting with 98 meet this condition, so the message about the Seattle Expo will print only on pages for customers located in or near Seattle.
Table 3-7 lists the values and constants you can use in expressions to refer to the various sections on a form or report. Group levels 3 through 10 (reports only) continue the numbering scheme shown here, but have no corresponding VBA constants.
Table 3-7. Values used to identify form and report sections in expressions
Setting
VBA constant
Description
0
acDetail
Detail section
1
acHeader
Form or report header section
2
acFooter
Form or report footer section
3
acPageHeader
Form or report page