Section Format Dynamically Display Page Numbers

The Format event procedure executes between the occurrences of when Access determines what data belongs in a report section, but before that data is formatted for printing. This procedure is suitable for making changes that affect page layout, such as displaying or hiding controls or for the creation of complex running calculations that span multiple groupings.

It is important to note that this event procedure executes for every section, including any sections that are not printed. To contrast this point, note that the Print procedure executes only when sections are printed. Unprinted sections will not execute, which may cause a problem for calculated running totals. Be sure to validate all numbers that are generated when performing a calculation on any report.

When using the Format procedure, the section (Detail, Header, or Footer) that is used will not only dictate when the event procedure should execute, but also what data is exposed at the time of execution. Exposed is a term that specifies that it is available for use by the developer. The following table describes these differences.

Section

Executes

Data Exposed

Detail

Just before each record is formatted

Current record

Group Header

Just before each new Group Header is formatted

Data in the group header First record of the Detail ection

Group Footer

Just before each new Group Footer is formatted

Data in the group footer Last record of the Detail section

In the following sample code, the format procedure is used to change an attribute about the report page footer section.

Private Sub PageFooter_Format(Cancel As Integer, _ FormatCount As Integer) If Me.Page Mod 2=0 Then

'Display the even page number on the Right Side Me.txtPageOdd.Visible = False Me.txtPageEven.Visible = True Else

'Display the odd page number on the Left side Me.txtPageOdd.Visible = True Me.txtPageEven.Visible = False End If End Sub

In the above sample, it is assumed that there are two text box controls placed within the Page Footer. They are identical in their data properties, as both have the following controlsource:

When the word "Page" is used on a report, it refers to the current page number. (This is what is known as a reserved word, and should only be used for the intrinsic purpose.) For the expression specified as the controlsource, it concatenates the literal word "Page" to the number of the current page of the report.

As far as formatting of the two text boxes are concerned, txtPageOdd is placed on the left side of the page footer, and is left-justified. txtPageEven is placed on the right side, and is right-justified. With this setup, turn your focus back to the code in the event procedure, and notice that sometimes the odd page number is visible, and other times the even page number is visible. The effect that this gives is that the page number will either be on the left or right side of the page, depending if the number is odd or even.

The way that this is controlled is through the use of the Mod operator. The keyword mod is short for modulus and returns only the remainder of a division calculation between two expressions. As applied in this situation, the page number is divided by the number 2, and the remainder is compared to zero. If a number is divided by 2, and has a remainder, then it is not an even number. The converse is that the number is even. So, the code determines if the page number is odd or even and then displays the proper text box to show the page number on the appropriate side of the page.

0 0

Post a comment