ACC: Keeping Subreport or Text Box Data Together on a Page

Last reviewed: October 24, 1997
Article ID: Q145780
The information in this article applies to:
  • Microsoft Access versions 7.0, 97

SUMMARY

Moderate: Requires basic macro, coding, and interoperability skills.

Subreports and text box controls do not have a KeepTogether property to prevent the controls' data from printing across two or more pages. However, you can simulate the KeepTogether property behavior by creating "artificial" group sections and placing the subreport and text box controls in these sections. Then, you can set the group's KeepTogether property to Yes, which causes Microsoft Access to test the layout of the controls before printing them, and keep the text together on one page.

NOTE: This article explains a technique demonstrated in the sample files, RptSampl.exe (for Microsoft Access for Windows 95 version 7.0) and RptSmp97.exe (for Microsoft Access 97). For information about how to obtain these sample files, please see the following articles in the Microsoft Knowledge Base:

   ARTICLE-ID: Q145777
   TITLE     : ACC95: Microsoft Access Sample Reports Available on MSL

   ARTICLE-ID: Q175072
   TITLE     : ACC97: Microsoft Access 97 Sample Reports Available on MSL

MORE INFORMATION

The following steps use the sample database Northwind.mdb to demonstrate how you can use report sections to simulate a KeepTogether property for subreport and text box controls.

Creating a Subreport

  1. Open the sample database Northwind.mdb.

  2. Run the Report Wizard to create a report based on the Products table.

  3. In the "Which Fields do you want on your report?" box, add the following fields to the Selected Fields list:

          ProductName
          QuantityPerUnit
          UnitPrice
    

  4. Click Finish.

  5. Close the report.

  6. In the Database window, use the right mouse button to click the Products report, and then click Rename on the menu that appears. Type the following name for the report, and then press ENTER:

          srptProducts
    

Creating a Main Report

  1. Run the Report Wizard to create a report based on the Categories table.

  2. In the "Which Fields do you want on your report?" box, add all of the fields to the Selected Fields list.

  3. Click Finish.

  4. Close the report.

  5. In the Database window, use the right mouse button to click the Categories report, and then click Rename on the menu that appears. Type the following name for the report, and then press ENTER:

          rptCategories
    

  6. Open the rptCategories report in Design View.

  7. On the View menu, click Sorting And Grouping.

  8. In the Field/Expression dialog box, select CategoryID in the first row and type the following expression in the second row (below CategoryID):

          =1
    

  9. For the "=1" expression, select "Yes" in the GroupFooter property.

Adding the Subreport to the Main Report

  1. Drag the srptProducts report from the Database window and drop it onto "=1" group footer to create the subreport container.

  2. Change the following subreport control properties:

          SubReport control:
    
            Name: Products
            ControlSource: Report.srptProducts
            LinkChildFields: CategoryID
            LinkMasterFields: CategoryID
            CanGrow: Yes
            Left: 0
            Top: 0
            Width: 7
            Height: .25
    
    

  3. Select the "=1" group footer and set its properties as follows:

          ProductsGroup Footer:
    
            KeepTogether: Yes
            CanGrow: Yes
            Height: .25
    
    

  4. Preview the rptCategories report. Note that the subreport data is printed on one page, rather than across two or more pages.

Additional Notes

  • Creating an "artificial" group (as demonstrated in this article) works well for subreport controls, as well as for text box controls bound to Memo fields. When Memo fields are printing, there are no events triggered until the Memo field is finished. Therefore, you cannot tell if the Memo field will split data between two pages. By placing the Memo field in its own "artificial" group, the group's KeepTogether property will test the layout of the Memo field before printing and keep the text together on one page when printing.
  • If your subreport or text box does not grow, you can size the subreport control in the main report to be the same size as the subreport itself. Then, set the KeepTogether property of the subreport's section on the main report to No. When you print the report, Microsoft Access will not generate a page break in the middle of a control.
  • If your subreport or text box does grow, you should place the control into its own section and set the KeepTogether property for the subreport's section in the main report to Yes.
Keywords          : RptProp kbusage
Version           : 7.0 97
Platform          : WINDOWS
Hardware          : x86
Issue type        : kbinfo


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: October 24, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.