Introduction.
Hiding report lines or report sections conditionally at runtime may not be an everyday requirement. However, when it becomes necessary, it’s interesting to know how to do it with the help of VBA programs. One feature I particularly like about MS Access reports is the ability to write programs directly in the report itself—especially for highlighting critical information so that users’ attention is drawn to specific details. You can see an interesting example in an earlier post titled Highlighting Reports.
Normally, to suppress something from printing on a report, we would use a query to filter out unwanted items from the source data before previewing or printing. Frankly, using a query is the best method compared to what I’m showing here. To achieve the same result on the report without filtering the source data, we need to use a few tricks—after all, this is all about Tips & Tricks, right?
If you enjoy working with MS Access programs, read on. The code itself is not complicated; a few simple lines are enough, and even a VBA beginner can understand them easily.
It’s enjoyable to do something different, not only to break the monotony of repeating the same tasks but also to gain better insight into doing things more efficiently next time.
Design a Sample Report.
We will create a sample Report quickly to try out this Trick.
Import the Orders and Employees tables from C:\Program Files\Microsoft Office\Office11\Samples\Northwind.mdb.
We won’t be using the Employees table directly in the report. Without the Employees table in the database, only the Employee IDs will be displayed when designing the report using the Orders table. There is a reference to the Employees table in the EmployeeID field of the Orders table (for example, the combo box uses the Employees table).
To create the report:
-
Click on the Orders table and select Report from the Insert menu.
-
Choose Report Wizard from the displayed list and click OK.
-
From the Available Fields list, select the following data fields and move them one by one to the report (fields list continues…).
Selected Fields List:
- OrderID
- CustomerID
- EmployeeID
- OrderDate
- RequiredDate
Click Next, select CustomerID as Grouping Level, and move it to the right. Click Next Command Button.
Select OrderID for Sorting in the first Sort Control and click Finish to create the sample Report.
An image of the sample Report is given below:
Open the Report in Print Preview and check the Order Numbers appearing under the first three Customer Groups on the Report.
We will attempt to hide three different Orders (Order IDs 10702, 10625 & 10573 from ALFKI, ANATR & ANTON Customer#39 List, respectively) from appearing on the Report with the following simple lines of Code:
Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer) If [OrderID] = 10702 Or [OrderID] = 10625 Or [OrderID] = 10573 Then Report.Section(acDetail).Visible = False Else Report.Section(acDetail).Visible = True End If End SubThe OnFormat() Event Procedure
If you look at the above code, you can see that the code runs under the On Format Event Procedure in the Detail Section of the Report.
Open your Report in Design View and display the Code Module (View -> Code).
Copy and Paste the above Code into the Report Module.
Select Close and Return to Microsoft Office Access from the File Menu to close the VBA Window and return to the Design View of the Report.
Or you can press Alt+F11 to Toggle between the Database Window and the VBA Window. Visit the Page Keyboard Shortcuts to learn more about Keyboard Shortcuts.
Click on the Detail Section of the Report and display the Property Sheet (View -> Properties).
Check the On Format Event Property, and you can see that the entry [Event Procedure] is appearing there, indicating that the Code that we have pasted Runs on the Format Event of the Report.
There are two more Events associated with the Report's Printing or Previewing action: Print Event and Retreat Event.
The Report Formatting Passes
MS Access makes two passes over a report before it is actually displayed or printed. The first, the Format pass, lays out the contents of each report section and performs any necessary calculations for summary information (we’ll cover this in more detail in forthcoming articles). The second pass, the Print pass, prints the report’s contents before previewing or sending it to the printer.
The Retreat event occurs when you move the page preview to an earlier page. During this event, the Format action runs a second time for the retreated page and the FormatCount parameter.
is incremented by one. This can have unintended effects if calculations are being performed in this procedure. We will examine this aspect further in upcoming articles.
- Save the Report and open it in Print Preview.
- Check whether the Report Lines associated with OrderIDs 10702, 10625 & 10573 are still appearing on the Report or not.
The Parameter Table
We were using Constant values for OrderIDs in the Program to check and suppress the Report lines from appearing on the Report. We cannot expect the User to open the Report in Design View and change the Program every time to change the OrderIDs, if it becomes necessary, before printing the Report. Besides, there may be more than two or three Orders to hide this way. We must provide a much simpler method to the User to input the OrderIDs as parameters to the Program and run the Report based on that.
We will create a parameter table with the key in OrderIDs, read it in the program, compare it to the Report Order IDs, and skip those lines on the Report for matching cases.
- Create a Table with the name OrderParam with a single Field with the name OID with Data Type Number and Field Size Long Integer.
- Select Primary Key from the Edit Menu to define this field as a Primary Key Field. This will prevent duplicate values from going into the Parameter Table and make it easier to cross-check OrderIDs from the Report.
Save the Table and open it in Data Sheet View.
- Key in the OrderIDs 10702, 10625, and 10573 (or any other OrderIDs you would like to hide) in the OrderParam Table.
Design a Continuous Form for the Table and create a Command Button at the Footer Section of the Form with HyperLinks to the Orders Report so that the User can launch the Report from the Parameter Screen itself.
The VBA Code
- Copy and paste the following Code in Report#39's Code Module, replacing the earlier Code:
'Global declarations Dim cdb As Database, rst As Recordset Private Sub Report_Open(Cancel As Integer) Set cdb = CurrentDb Set rst = cdb.OpenRecordset("OrderParam") rst.Index = "PrimaryKey" End Sub Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer) rst.Seek "=", [OrderID] If Not rst.NoMatch Then Report.Section(acDetail).Visible = False Else Report.Section(acDetail).Visible = True End If End Sub Private Sub Report_Close() rst.Close Set rst = Nothing Set cdb = Nothing End Sub - Save the Report with the new Code and open it in Print Preview.
- Look for the OrderIDs in the OrderParam Table to check whether they are really suppressed from the Report or not.
Let us examine what we did in the above Code.
The VBA Code Review
We have declared the Database and Recordset objects at the global level of the report module so they can be referenced in all other subroutines.
In the report’s Open event procedure, we open the OrderParam table, activate its primary key index, and keep it in memory.
During the Detail Section Format event procedure, we cross-check each OrderID against the contents of the OrderParam table. If an OrderID matches a value in the table, the corresponding report line is hidden. This process continues for all records in the report.
The OrderParam recordset is closed in the report’s Close event procedure.
Because this checking process references an external table for each record in the report’s source data, there may be a slight delay before the preview or print action completes.
Next, we will explore how this method works with Customer Level Summary Totals in the Customer Footer Section of the Report.
-










