Access Cookbook - Ken Getz [71]
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.
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.
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
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
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
Figure 3-24. The footer for the even pages of rptEvenOdd
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.)
3.11. Make a Vertical Line the Same Height as a CanGrow/CanShrink Control
Problem
You have a control on a report that has its CanShrink and CanGrow properties set to Yes so it can grow or shrink to accommodate different amounts of text. You placed a vertical line to the left of the control, and you want it to be the same height as the control. Is there a way you can synchronize the height of the two controls?
Solution
If you place a line on a report using the Line tool, it will always be the same size. To make a line change its height to match the height of another control (or group of controls), you need to use the Line method in a procedure attached to the Print event of a report