The Cold Fusion Web Database Construction Kit

Previous chapterNext chapterContents


- 14 -
The Report Writer



Ben Forta

Using CFML, the Cold Fusion Markup Language, and its supporting functions, you are able to create dynamic reports that are driven by queries to live data. Creating reports manually, however, is no substitute for an industrial strength report writer.

To enable you to easily create powerful dynamic reports, Crystal Reports Professional version 5 is bundled with Cold Fusion. With Crystal Reports, you can create sophisticated reports, charts, and graphs that can be embedded into your Cold Fusion templates.

In this chapter, you will learn how to create basic reports with Crystal Reports Professional and how to embed reports into Cold Fusion.

Introducing Crystal Reports Professional

Crystal Reports Professional has long been recognized as one of the most powerful and easy-to-use report writers available. This is why so many software vendors, including Allaire, have chosen it as the report writer to be bundled with their applications.

Crystal Reports Professional is popular because:

It is the last feature in this list that makes Crystal Reports Professional so appealing to Cold Fusion developers.


For help with installing Crystal Reports Professional, see Chapter 4, "Installing and Administering Cold Fusion."


NOTE: Crystal Report does not have to be installed on the computer running your web server and Cold Fusion. All the files that Cold Fusion uses to interact with Crystal Reports' RPT files are installed during the Cold Fusion installation.

So, if you're ready, let's create a report.


A Note from the Author

Crystal Reports Professional version 5 is an extremely powerful and capable report writer. Complete coverage of all of its features is beyond the scope of this book.
This chapter will not teach you everything there is to know about Crystal Reports Professional. Instead, it'll teach you how to create a basic report with the report writer and how to execute that report from within your Cold Fusion templates.
Crystal Reports Professional comes with complete online help and step-by-step tutorials. If you are planning on using the report writer, I strongly urge you to use these tools.
Crystal Reports Professional only reads your data files, it does not write to them. Therefore, there is nothing that you can do within the report writer that will corrupt or damage any data, so don't be afraid to experiment.


Creating Reports with Crystal Reports Professional

To learn how to use the report writer, we'll start by creating a simple report--an employee directory.


TIP: You can use Crystal Reports on any computer, not just on the web server machine, but make sure that the ODBC data source you'll use is set up exactly the same way it is on the web server machine. This way, you can be sure that reports created on one computer will work on another.

For starters, let's load the Crystal Reports program. Click its icon to display the welcome screen as shown in Figure 14.1. You'll be presented with two options: one to create a new report and the other to open an existing report. Select the New Report option to display the Report Gallery, shown in Figure 14.2. The Report Gallery displays eight popular report types from which you may select but also allows you to create your own type.

Figure 14.1  The Crystal Reports Professional welcome screen prompts you to either create a new report or open an existing one.

Figure 14.2  The Report Gallery allows you to pick a report type or create a custom type of your own.

We're going to create a standard report, so select the Standard button to display the Create Report Expert.

The experts are interactive tools that walk you through much of the report design process. Crystal Reports' experts are made up of multiple screens in a tab dialog, as shown in Figure 14.3. You may jump to any screen by clicking the tab at the top. Alternatively, you may use the << Back and Next >> buttons to walk through the screens while building your report.

Figure 14.3  The Create Report Expert walks you through the process of creating a report.

To build our report, follow these steps:

1. The first thing you need to do is specify the tables to be used in this report. We'll be using data from an ODBC data source, so select the SQL/ODBC option from the list of data types.

2. You'll be prompted for a server type. Servers are what Crystal Reports calls any data source, including ODBC data source. The list of servers includes both ODBC data sources (they all have the prefix ODBC) and other sources available to you. We'll be using the A2Z ODBC data source, so select ODBC -A2Z from the list, and then choose OK.

3. Now that you have specified a data source, Crystal Reports can prompt you for the tables to include in the report. For our first report, we'll select the "Employees" table and the "Departments" table, so select each of those tables and choose Add. Once you have selected both files, choose Done.

4. Because we selected more than one table for our report, the Expert now wants to know how these tables are related. To set relationships between tables, you must locate the related columns in each table and then drag one to the other to link them. The columns that link our two tables are the "DepartmentID" column in the "Employees" table, and the "ID" column in the "Departments" table. Scroll down the list of columns in the "Employees" table until you find the "DepartmentID" column and click it. Drag it to the "ID" column in the "Departments" table. The Expert draws a white line showing the link between the two tables, as shown in Figure 14.4. (You may choose Arrange to arrange the tables to clearly see the links.) When you have finished creating the link, choose Next >>.

5. Now the Expert needs to know which fields to include in the report. The left list is the Database Fields list which contains all the available fields sorted by table and fields within a table, and the right list is the Report Fields list which contains any selected fields. To include fields in a report, select them from the left list, and click the Add -> button. Select these fields in this order: Employees.LastName, Employees.FirstName, Departments.Department, Employees.PhoneExtension.

6. When displaying your data in the final report, Crystal Reports uses the field name as the column heading, unless you specify alternative text. To specify column heading text, select a field from the Report Fields list and enter the desired text into the Column Heading field. Set the Employees.LastName field to "Last Name", Employees.FirstName to "First Name", leave the Departments.Department field as is, and set the Employees.PhoneExtension field to "Extension". When you have completed this step, choose Next >>.

7. Now you can specify a sort order. We want the list sorted by last name plus first name. Select Employees.LastName from the Report Fields list, and click the Add >> button to select that field. Now do the same for the Employees.LastName field. The selected fields are shown in the Group Fields list.

8. Our basic report does not require any of the options in the other Expert screens, so click the Preview Report to see what the finished report will look like. As shown in Figure 14.5, the report writer displays a preview of the finished report using live data retrieved from the specified tables.

9. Above the display area are two tabs entitled Design and Preview. The Preview tab, the tab currently selected, allows you to preview the finished report at any time. The Design tab is where you can modify the report layout. Choose the Design tab to show the report Design screen as shown in Figure 14.6. The Design screen is divided into groups (also called bands). The top group, the Page Header, contains information that will appear at the top of every page in the report. The bottom group, the Page Footer, contains information that will appear at the bottom of each page. The middle group is the Details group that contains the fields that will appear in the body of the report. The Details groups is repeated once for every row selected in the final report.

10. To format or change any field or text, you need to right-click it with your mouse. This action displays a context-sensitive menu containing options applicable to the selected field. We'll change the format of the column headers, so select all four column headers at once by clicking your mouse above the first column and then dragging the box outline over all four fields. When you release the mouse button, all four fields will be selected. Now right-click your mouse on any of the selected fields and select the Format Objects... option. Select the Font page, and set the Font to Arial, the Size to 12, and turn off Underline. Then choose OK. The Design view immediately reflects the changes you just made. (You may also switch to the Preview view at any time to see how the change will effect the final report.)

11. The Page Footer group contains a page number field. This is a very useful field for printed reports, but is useless for reports that will display as Web pages. When a report is output as a Web page, it is formatted as a single page, so we will remove that field. To remove it, click the field to select it and press the Delete key.

12. The last thing we need to do is save and name the report. Crystal Reports saves its reports in RPT files and each RPT file contains a single report. Select the Save option from the File menu and save the report as EMPLIST.RPT in the C:\A2Z\SCRIPTS directory.

Figure 14.4  The Expert graphically displays any links established between different tables.

Figure 14.5  Crystal Reports Professional previews reports using live data that enable you to see exactly what a finished report will look like.

Figure 14.6  Use the Design screen to make any changes to a report.

You've just created a complete report using the Crystal Reports report writer. You may print this report at any time by selecting Print from the File menu.


NOTE: Many of the report writer's formatting features cannot be supported by HTML, so some advanced formatting will be lost when the report output displays in a Web page. For example, Crystal Reports allows you to specify single, double, or mixed borders around text, all of which are rendered into HTML as standard TABLE borders. Before rolling out your reports for public use, make sure you test them thoroughly to ensure that the output is acceptable.

Embedding Reports into Cold Fusion Templates

Reports created with Crystal Reports Professional can easily be printed. Our interest is in its HTML publishing capabilities because Crystal Reports is most often used for printing.

Reports are embedded into Cold Fusion Templates using the <CFREPORT> tag. When Cold Fusion processes a template and encounters a <CFREPORT> tag, it executes a Crystal Reports engine which processes the report and creates an HTML version of the report output.


NOTE: Cold Fusion returns an error message if you try to process a report that is already open in Crystal Reports. You must close any reports in the report writer before using them with Cold Fusion.

We'll now create a template to process the report we just created. Listing 14.1 contains the code to process report EMPLIST.RPT. As you can see, the code listing is standard HTML, except for the <CFREPORT> tag. <CFREPORT> requires a single attribute, REPORT, which contains the name (and fully qualified path) of the report to execute. Create a template containing the code in Listing 14.1 and save it as EMPLIST.CFM in the C:\A2Z\SCRIPTS directory.

Listing 14.1  EMPLIST.CFM--Employee List Displayed Using a Crystal Reports Professional Report

<HTML>
<HEAD>
<TITLE>Employee List</TITLE>
<HEAD>
<BODY>
<CFREPORT REPORT="emplist.rpt">
</CFREPORT>
</BODY>
</HTML>

Now let's test the report. Load your web browser and go to URL http://yourservername/a2z/emplist.cfm. After a few seconds of processing, your browser display should look like the one shown in Figure 14.7.

Figure 14.7  Cold Fusion executes a Crystal Reports Professional engine to process embedded reports.

Customizing Reports On-the-Fly

Crystal Reports Professional enables you to interactively create powerful reports which you can easily embed into Cold Fusion.

Cold Fusion also allows you to pass parameters to the Crystal Reports processing engine enabling you to customize the reports on-the-fly.

For example, the phone list report we created earlier displays all employees in all departments. How could you create a report to display the employees in a single department?

You could create multiple reports and specify a filter condition in each one. One report would filter only the Sales department employees, another would filter just the Shipping & Receiving department employees, and so on.

It can be done, but it is definitely not a scaleable or manageable solution. A better alternative would be to use the same report and modify it on-the-fly by passing additional information to it.

The code in Listing 14.2 shows how this is done. The <CFREPORT> tag is the same, but we've added a line between the <CFREPORT> and </CFREPORT> tags. The code {Departments.Department} = "Sales" is passed to the Crystal Reports engine as a selection filter. In this case, only employees who have a Departments.Department value of "Sales" will be included in the report.

Listing 14.2  EMPSALES.CFM--Crystal Reports with Passed Filter Condition

<HTML>
<HEAD>
<TITLE>Employee List</TITLE>
<HEAD>
<BODY>
<CFREPORT REPORT="emplist.rpt">
 {Departments.Department} = "Sales"
</CFREPORT>
</BODY>
</HTML>


NOTE: Field names passed to Crystal Reports must be fully qualified with the table name and must be enclosed within curly braces.

The selection criteria may even be made up of Cold Fusion tags, functions, and fields. For example, instead of hard coding the filter to a department called "Sales," we could have compared it to a passed field name. In fact, the entire filter condition could be contained with a <CFIF> conditional statement so that the same template could display as many different employee lists as needed.


The Cold Fusion interface to the Crystal Reports Engine enables you to specify options and attributes other than the ones discussed in this chapter. See the <CFREPORT> section of Appendix A, "Cold Fusion Reference," for more details.

From Here...

In this chapter, you learned the basics of using Crystal Reports Professional version 5 and how to embed reports into Cold Fusion templates. You also learned how to pass filter conditions to the Crystal Reports engine enabling you to easily reuse reports to display related but different data sets.

Crystal Reports is a powerful and capable report writing tool, and one well worth getting to know.


Previous chapterNext chapterContents

© Copyright, Macmillan Computer Publishing. All rights reserved.