When you have large paper report, it is sometimes easier for your users to navigate through the report if you include a table of contents. Using Reports Builder, you can generate a table of contents that displays at the beginning of your paper report.

Normally, Reports Builder formats the report starting with the Header section, then the Main section, followed by the Trailer section. With the functionality demonstrated in this example report, Reports Builder can format any section first to create information that is only known at the time of formatting, such as page numbers, then use that information in the formatting of a previous section.

In this example, you will use the built-in procedure called SRW.SET_FORMAT_ORDER to generate the Main section of the report first, then the Header and Trailer sections. Doing so enables you to generate the page numbers for the report in the Main section, then display the page numbers in the table of contents in the Header section.

Note:

Although you can use SRW.SET_FORMAT_ORDER to change the order in which the report is formatted, the report will still display the Header section first, then the Main section, and finally the Trailer section. You can use SRW.SET_FORMAT_ORDER to simply change the formatting order, but not the display order of the report sections.

You will use the After Parameter Form trigger and a format trigger to place the page numbers temporarily in a table you create in the database. You will then create a simple tabular report that displays at the beginning of your report, that lists the category and its page number. Finally, you will create an index that simply lists the record and the page number in the Trailer section of the report.

In this example, you will generate a list of all the e-mail addresses for your customer database, which contains thousands of records. First, you want to categorize the customers (in this case, by country), then generate a table of contents so you can easily find the e-mail address for the desired customer. Then, you will learn how to create an index for your report.

To see a sample table of contents and index report, open the examples folder named tocindex, then open the Oracle Reports example named formatorder_index.rdf. For details on how to open it, see "Accessing the Example Reports" in the Preface.

35.1 Prerequisites for this example

To build the example in this chapter, you must have the example files we have provided (see "Example Scenario", above), as well as access to the Sales History sample schema provided with the Oracle Database. If you do not know if you have access to this sample schema, contact your database administrator.

35.2 Create a simple table of contents

The steps in this section will show you how to create a basic table of contents for a large, multipage report. The table of contents will look like the following:

35.2.1 Create a table in the database to hold the TOC data

The steps in this section will show you how to create a table in the database that will hold the page numbers for the records you want to list in the table of contents. If you are not sure whether you have the privileges to create a table in the database, contact the database administrator.

Connect to the Sales History schema of the database (for example, use the sh/sh@database_name connect string).

At the SQL prompt, type the following line:

create table toc_example (topic varchar2(100), page number);

Press Enter.

You should see a notification that the table has been created.

Exit SQL*Plus.

35.2.2 Create a group above report

The steps in this section will show you how to build a simple group above report in Reports Builder. This group above report will display the country name, then the customers and customer e-mail addresses under each country name. The table of contents you create will then be based on the country name in this report.

To create a simple group above report:

In Reports Builder, choose File > New > Report, then choose to create the report manually.

In the Data Model view that displays, right-click on the canvas, then choose Report Wizard from the pop-up menu.

Copy and paste the code from the provided text file called formatorder_code.txt into the Data Source definition field.

Click Query Builder to build the query without entering any code manually.

Type the code in the Data Source definition field.

Click Next.

Note:

If you are not already connected to a database, you will be prompted to connect to the database when you click Query Builder or Next. Ensure that you connect to a database that has the appropriate schema for this example. Section 35.1, "Prerequisites for this example" describes the sample schema requirements for this example.

On the Groups page, click COUNTRY_NAME, then click > to move the field to the Group Fields list.

Click Next.

On the Fields page, click >> to move all fields to the Displayed Fields list, then click Next.

On the Totals page, click Next.

On the Labels page, click Next.

On the Template page, click Finish.

In the toolbar, click the Data Model view button. The data model should look like this:

Save your report as formatorder_your_initials.rdf. You have now created the data model and the basic layout for your report.

35.2.3 Create an After Parameter Form trigger and a format trigger

The steps in this section will show you how to create two triggers. The first trigger will use the SRW.SET_FORMAT_ORDER built-in procedure to format the Main section of the report first, then the Header and Trailer sections. The second trigger will fetch the number of the page on which each country name displays, and place that information into the table you created in Section 35.2.1, "Create a table in the database to hold the TOC data".

35.2.3.1 Create an After Parameter Form trigger

An After Parameter Form trigger is a function that executes after the parameter form is executed. In this section, you will set the trigger to change the order of the report execution, so that the Main section runs first.

To create an After Parameter Form trigger:

In the Object Navigator, under your report name (FORMATORDER_your_initials), expand the Report Triggers node.

Right-click AFTER PARAMETER FORM, then choose PL/SQL Editor from the pop-up menu.

In the PL/SQL Editor, use the template to enter the following PL/SQL code:

You can also copy and paste the code from the provided file, formatorder_code.txt.

Click Compile.

Once the code has compiled, click Close.

35.2.3.2 Create a format trigger

In this section, you will create a format trigger based on the field F_COUNTRY_NAME. This field displays the country name. This format trigger will fetch the page number for each country name, so that the table of contents will enable the user to navigate to various parts of the report based on the name of the country.

To create a format trigger:

In the Object Navigator, under the Paper Layout node for your report, navigate to Main Section > Body > M_G_1_GRPFR > R_G_1, then click F_COUNTRY_NAME.

Tip:

If you cannot find this field, use the Find field at the top of the Object Navigator.

Choose Tools > Property Inspector to display the Property Inspector for this field.

Under Advanced Layout, double-click the Format Trigger property field to display the PL/SQL Editor.

You can also copy and paste this code from the provided file, formatorder_code.txt.

Click Compile.

Once the code is compiled, click Close, then close the Property Inspector.

Save your report.

35.2.4 Create a second query in the data model

The steps in this section will show you how to create a second query in the data model with a formula column that calculates the page range for the data under each country name. This query will fetch the information from the database table you created in Section 35.2.1, "Create a table in the database to hold the TOC data". You will later create a tabular layout to display this information in your table of contents.

To create a second query with a formula column:

In the Data Model view, click the SQL Query tool in the tool palette then click in an open area of the Data Model view, somewhere to the right of query Q_1, to display the SQL Query Statement dialog box.

In the SQL Query Statement field, enter the following SELECT statement:

You can also copy and paste this code from the provided file, called formatorder_code.txt.

Click Compile.

Once the code is compiled, click Close.

Save the report.

35.2.5 Create a report block to display the table of contents

The steps in this section will show you how to create a tabular report block in the Header section of your report. This report block will display the headings in the table of contents (in this example, the country name), and the page range where the information can be found in the report.

To create a tabular report block in the Header section:

In the Paper Layout view, click the Header Section button in the toolbar.

Click the Report Block tool in the tool palette, then draw an area about about 5 inches (12.5 centimeters) wide and 1.5 inches (4 centimeters) high in the Paper Layout view. When you release the mouse button, the Report Block Wizard displays.

On the Style page, select Tabular, then click Next.

On the Groups page, click G_TOPIC and click Down to move it to the Displayed Groups list, then click Next.

On the Fields page, click each of the following fields in the Available Fields list, then click the right arrow (>) to move them to the Displayed Fields list, then click Next:

35.2.6 Run your simple table of contents report to paper

To run your report to paper:

Click the Run Paper Layout button in the toolbar.

Note:

To generate the table of contents (TOC), you must click the Run Paper Layout button. If you click the Paper Design view button, the change of format order will not take effect, thus the TOC will not be generated.

The table of contents displays on the first page of the report, and looks something like the following:

35.3 Create an index

In this section, you will create an index that displays at the end of your report. This index will simply display the customer's name and the page number on which the name appears. This indexing technique is useful when users want to find a specific customer's name, but are not sure what category (for example, country) to reference.

35.3.1 Create a table in the database to hold the information for the index

The steps in this section will show you how to create a table in the database that will hold the page numbers for the items you want to list in the index. If you are not sure if you can create a table in the database, contact the database administrator.

35.3.2 Create a format trigger

The steps in this section will show you how to create a format trigger based on the customer's last name. You will use the SRW.GET_PAGE_NUM built-in function to find the page number for the current customer, and insert that page number into the table you created in the previous section.

To create a format trigger:

In Reports Builder, open the report, formatorder_your_initials.rdf.

Click the Paper Layout button in the toolbar to display the Paper Layout view.

In the Paper Layout view, right-click the field F_CUST_LAST_NAME, then choose Property Inspector from the pop-up menu.

Under Advanced Layout, double-click the Format Trigger property field to display the PL/SQL Editor.

You can enter this code by copying and pasting it from the provided text file called formatorder_code.txt.

Click Compile.

If there are compilation errors, match your code to the code we have provided (either in the example RDF file or in this chapter), then compile it again.

Once there are no compilation errors, click Close.

Save your report as formatorder_index_your_initials.rdf.

35.3.3 Add a query to the data model

The steps in this section will show you how to add a query to your data model that will retrieve the individual customer names and the page numbers for your index.

To add the query:

In the Data Model view, click the SQL Query tool in the tool palette then click in an open area of the Data Model view, somewhere to the right of query Q_1, to display the SQL Query Statement dialog box.

In the SQL Query Statement field, enter the following SELECT statement:

SELECT SUBSTR(TERM,1,1) INITIAL_LETTER, TERM, PAGE FROM INDEX_EXAMPLE ORDER BY TERM

Note:

You can enter this query in any of the following ways:

Copy and paste the code from the provided text file called formatorder_code.txt into the Data Source definition field.

Click Query Builder to build the query without entering any code manually.

Type the code in the Data Source definition field.

Click OK.

In the new query that displays in the Data Model view, click and drag the INITIAL_LETTER column above the rest of the query, so that the data model now looks like this:

35.3.4 Create a report block to display the index in the Trailer section

The steps in this section will show you how to display the index information in the Trailer section of your report.

To create a report block in the Trailer section:

In the Paper Layout view, click the Trailer Section button in the toolbar.

Choose Insert > Report Block.

On the Style page, select the Group Above radio button, then click Next.

On the Groups page, click G_2 (the name of the new group that contains the INITIAL_LETTER column), then click Down to specify the print direction.

In the Available Groups list, click G_INITIAL_LETTER, then click Down.

Click Next.

On the Fields page, click INITIAL_LETTER, then click > to move it to the Displayed Fields list.

Move TERM and PAGE to the Displayed Fields list.

Click Next, then Next again, then click Finish.

Save your report.

35.3.5 Run your report to paper

Click the Run Paper Layout button in the toolbar. The index displays on the last pages of the report. We chose page 2402, which (in our example) looks like the following:

Note:

To generate the table of contents (TOC), you must click the Run Paper Layout button. If you click the Paper Design view button, the change of format order will not take effect, thus the TOC will not be generated.

The data on page 2402 in your report may not appear the same as ours, depending on the amount and type of data you have in the schema.

35.4 Summary

Congratulations! You have successfully created a report with a table of contents and an index. You now know how to:

create a table of contents (TOC) and index for a large paper report.

use the SRW.SET_FORMAT_ORDER built-in procedure to change the order in which the sections of a report are executed.

use the SRW.GET_PAGE_NUM built-in procedure to retrieve the page number for a specific record.

create a new table in your database to hold the necessary page numbers.

create a simple group above report using the Report Wizard.

manually create a simple data model to retrieve the information for your report.

create a format trigger to generate the necessary page numbers for an index.

use the Report Block Wizard to create a group report that displays the index in the trailer of a report.

For more information on any of the wizards, views, or properties used in this example, refer to the Oracle Reports online Help, which is available in Reports Builder or hosted on the Oracle Technology Network (OTN), as described in Section 3.1.1, "Using the Oracle Reports online Help".