Microsoft Access Tips for Serious Users

Provided by Allen Browne.


Reports: Page Totals

Each section of a report has a Format event, where you can alter properties such as Visible, Color, Size, etc in a manner similar to the Current in a form. (See Runtime Properties: Forms for details.) For example to force a page break when certain conditions are met, include a PageBreak control in the Detail Section and toggle its Visible property. In addition, the Print event can be used to perform tasks like adding the current record to a running total.

You have an Amount field, and want to display the Amount total for that page in the Page Footer. Totals are not normally available in the Page Footer, but the task requires just four lines of code!

  1. In the PageHeader's Format event procedure, add:
        curTotal = 0             'Reset the sum to zero each new Page.
  2. In the DetailSection's Print event, add:
        If PrintCount = 1 Then curTotal = curTotal + Me.Amount
  3. Place an unbound control called PageTotal in the Page Footer. In the PageFooter's Format, add:
        Me.PageTotal = curTotal
  4. In the Code Window under Declarations enter:
        Option Explicit          'Optional, but recommended for every module.
        Dim curTotal As Currency 'Variable to sum [Amount] over a Page.

That's it!


Home Index of tips Top