In a sorting and grouping report, where each group header can contain
information that may be duplicated from the previous group header, you can
use the following code to hide duplicate information in successive group
headers.
CAUTION: If you follow the steps in this example, you modify the sample database Northwind.mdb. You may want to back up the Northwind.mdb file and follow these steps on a copy of the database.
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.
- Start Microsoft Access and open the sample database Northwind.mdb.
- Create the following new select query in Design view based on the Categories, Products, and Order Details tables:
Field: CategoryName
Table: Categories
Field: ProductName
Table: Products
Field: OrderID
Table: Order Details
Criteria: <10300
Field: Quantity
Table: Order Details
- Save the query as qryOrders, and then close it.
- Create a new module. In the module Declarations section, declare a global variable called DupeHeader:
Global DupeHeader as String
- Save the module as basPrint, and then close it.
- Create a new report in Design view based on the qryOrders query.
- On the View menu, click Sorting and Grouping.
- In the first row of the Sorting and Grouping dialog box, select CategoryName in the Field/Expression box.
- In the second row of the Sorting and Grouping dialog box, select ProductName, and then set the GroupHeader property to Yes.
- Close the Sorting and Grouping dialog box.
- On the View menu, click Field List.
- Drag CategoryName and ProductName from the field list to the ProductName Header section of the report.
- Drag OrderID and Quantity to the Detail section of the report.
- If the property sheet is not visible, click Properties on the View menu.
- Set the Height property of the Detail section to .25".
- Set the OnFormat property of the ProductName header to the following event procedure:
Private Sub GroupHeader0_Format(Cancel As Integer, FormatCount _
As Integer)
If DupeHeader = Me![CategoryName] Then
Me![CategoryName].Visible = False
Else
Me![CategoryName].Visible = True
End If
DupeHeader = Me![CategoryName]
End Sub
- Save the report as rptTest, and then close it.
- Open the report in Print Preview and scroll through it. Any
duplication of the CategoryName field in successive ProductName headers is not visible.