Recipe 3.10 Print Different Headers or Footers on Odd and Even Pages

3.10.1 Problem

Some of your reports are printed double-sided, and you would like to have mirror-image headers and footers on odd and even pages. How do you do this in Access?

3.10.2 Solution

This technique makes use of two sets of header and footer controls, one for odd pages and one for even pages. An event procedure run from the section's Format event uses the Page property and the Mod operator to determine whether the page is odd or even and makes the appropriate controls visible or invisible.

The following steps show you how to create your own report that prints different headers and footers on odd and even pages:

  1. Open the report you want to print double-sided (or even single-sided, with different odd and even headers and footers).

  2. Make a copy of the header control, and place one of the copies of the control on the left of the header and the other on the right. Make the lefthand control left-aligned (to print on even-numbered pages) and the righthand control right-aligned (to print on odd-numbered pages).

  3. Create an event procedure attached to the OnFormat property of the report's page header section. In the event procedure, enter code similar to the following:

    Private Sub PageHeader_Format(Cancel As Integer, FormatCount As Integer)     On Error GoTo PageHeader_FormatError          Dim fIsEven As Boolean          fIsEven = acbIsEven(Me.Page)          Me.lblTitleLeft.Visible = Not fIsEven     Me.lblTitleRight.Visible = fIsEven      End Sub

    You'll need to replace the controls in the event procedure with the names of your controls.

  4. Make copies of the footer controls as well, and make a similar event procedure for the footer's OnFormat event property, referencing its left and right controls. In the event procedure, enter code similar to the following:

    Private Sub PageFooter_Format(Cancel As Integer, FormatCount As Integer)          Dim fIsEven As Boolean          fIsEven = acbIsEven(Me.Page)          Me.txtPageLeft.Visible = Not fIsEven     Me.txtPageRight.Visible = fIsEven     Me.txtPrintedOnLeft.Visible = fIsEven     Me.txtPrintedOnRight.Visible = Not fIsEven      End Sub

    Again, you'll need to replace the controls in the event procedure with the names of your controls.

  5. Without closing the module, add the following function to the form's module:

    Private Function acbIsEven(ByVal intValue As Integer) As Boolean     ' Return True if intValue is even, False otherwise.     acbIsEven = ((intValue Mod 2) = 0) End Function
  6. Save and execute the report to confirm that it performs as desired. The completed report is shown in design view in Figure 3-22.

Figure 3-22. rptEvenOdd in design view
figs/acb2_0322.gif

To see the sample report, load 03-10.MDB. Open rptEvenOdd in print preview mode; you should get a report that has one header and footer for odd pages (see Figure 3-23) and a different header and footer for even pages (see Figure 3-24).

Figure 3-23. The footer for the odd pages of rptEvenOdd
figs/acb2_0323.gif
Figure 3-24. The footer for the even pages of rptEvenOdd
figs/acb2_0324.gif

3.10.3 Discussion

The two event procedures call the acbIsEven function to determine whether the current page is even or odd, passing the current page number to the function. The current page number is determined by referencing the Page property of the report (Me.Page). acbIsEven uses the Mod operator, which returns the remainder when the page number is divided by 2, yielding 0 for even pages or 1 for odd pages. The following statement:

acbIsEven = ((intValue Mod 2) = 0)

returns True to the calling procedure if the page Mod 2 is 0 (i.e., if the page is even); otherwise, it returns False.

If you set fIsEven to the return value of acbIsEven, you can then set the visibility of the rest of the controls based on its value.

You can't see them in Figure 3-22, but there are four text boxes in the footer section of the example report. On the left side of the footer, the txtPagePrintedOnLeft control has been placed on top of the txtPageLeft control. On the right side of the footer, the txtPageRight control has been placed on top of the txtPrintedOnRight control. This works because only one set of controls (txtPagePrintedOnLeft and txtPageRight, or txtPageOnRight and txtPageLeft) are visible at the same time.

As an alternative to using two controls in the header of the report, you could use just one control that is as wide as the report and alternately set its TextAlign property to Left or Right based on the return value of acbIsEven. (You can't do this in the footer because of the need for two sets of controls with different alignments.)



Access Cookbook
Access Data Analysis Cookbook (Cookbooks)
ISBN: 0596101228
EAN: 2147483647
Year: 2003
Pages: 232

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net