Basic Tutorial Part 2

By Stephen Frye

This article is Part 2 of a 2-part tutorial on building reports in Lazarus using FortesReport. This tutorial will show how to build a master-detail style report.

Before Starting

As with the first tutorial, this chapter will use three extra sets of items not supplied with Lazarus:

If you don’t have these installed on your PC, refer to  Tutorial Part 1 for links to these resources and installation instructions.

What’s being built

The end result of this tutorial will be a Master-Detail report. For every item in the Master part of the report, there will be one or more related Detail items. This relationship is very common in database systems: Orders (Master) and Order Items (Detail) is an often-seen scenario. For this tutorial, we will report on the Sales (Detail) generated by the Sales Staff (Master) in the EMPLOYEES.FDB database included with the Firebird database engine.

Let’s Get Started

Start Lazarus and start a New Project. Since FortesReport uses a non-visible form to hold its components, add an extra form to the project for the report itself plus a Data Module to hold the database connections. Save the project, giving meaningful names to the forms and source files. The following names will be used in this tutorial:

Beginning with this tutorial, I’m adopting a naming convention recommended in a QuickReport tutorial: the name of the form holding the report is given the name of the report, and the report itself is named, simply, “Report”. I feel it will make it easier to keep track of reports in the IDE, the Obect Inspector and code – printing a report is then done with a self-explanatory line of code, e.g.:


Connect to the Database

First, we’ll add the components to connect the Lazarus application to the Firebird database, starting with the basic connection. From the Zeos Access tab in the Lazarus IDE, place a TZConnection component into the DataModule. Set the following properties:

Test the connection by setting the Connected property to true. If no errors occur, your connection is properly set.

Next are the components to retrieve data for the Master part of the report. Again from Zeos Access, place a TZReadOnlyQuery component in the DataModule, and set the following properties:

Test this by right-clicking qrySalesStaff, picking “Edit Fields” from the context menu, and then clicking on the “+” sign in the pop-up dialog. You should see the following fields in the list:

Select all of the fields in the list, and click on Create to add the fields to the Object Inspector. This is necessary to make the fields usable in your report.

Switch to the Data Access tab in Lazarus and place a TDataSource component in the DataModule. You’ll need to set the following properties:

Finally, add the components to retrieve data for the Detail part of the report. From Zeos Access, place a second TZReadOnlyQuery component in the DataModule, and set the following properties:

Switch to the Data Access tab in Lazarus and place a TDataSource component in the DataModule. You’ll need to set the following properties:

Build the Report

Now it’s time to actually start construction of the report. The report is a listing of Employees in the Firebird database who are classified as being part of the Sales departments, along with any sales they have generated which have actually been shipped. As with the report in Tutorial Part 1, the report will have a Page Header and Column Header along with the rows of information, and the report will be sorted by the employee’s Last Name (surname) and First Name (given name). Since most of the report-building basics were covered in Part 1, they will not be repeated here. When done your report should look something like this:

Place a TRLReport component onto rptMasterDetail from the FortesReport tab. Set its name to “Report”, and set the DataSource property to the Master DataSource: ModData.dsSalesStaff. This tutorial will be based on a Letter-size sheet of paper in Portrait orientation, but feel free to choose whatever PageSetup properties you wish. Expand the AllowedBands property and set btColumnHeader (column header), btDetail (detail information) and btHeader (page header) to True.

Now we add specific bands and controls to the report page:

Page Header

Add a TRLBand to the report page. Set the BandType to btHeader, and set its name as well: bndHeader. Drag the sizing handles to increase the height of the band.

Master-Level Data Fields

Place a new TRLBand underneath the Page Header band. Set its BandType to btDetail. Drag the sizing handles to make it tall enough to accommodate the Master-table data fields and column headers for the Detail-table data fields. A height of 3 cm. should be plenty of room for the design work; once you’re satisfied with your layout you can shrink it back down to what’s really needed.

Detail-Level Data Fields

Now it’s time to add the detail items. In addition to the detail data, we’ll also add summary components to show the total number and total value of the shipped orders for each sales person.

OK, that’s it! There’s nothing else to be added to the design of the report. If you’ve followed the Tutorial precisely, your form in design-mode should be similar to this:

We’re not quite done yet, however. If you right-click the report and select Preview, you might see something like this:

Yuck! What’s with those numbers under the Value column? They don’t look like anything useful! OK, the last step in the design of this report is to format the Value column so that it is easy for the human eye to read:

For my report, I entered this string into the DisplayFormat property: #,###,##0.00. This formats the data with commas as the thousands separator, a period for the decimal point, and two places to the right of the decimal. Making either of the above formatting settings will set the format for both the data rows and the summary field.

Finish the Application

Now it’s time to add the user-interface components to the main form which allow the report to be Previewed or Printed. We also want to make sure that the report-design form is not visible when the program is run. There are three basic steps needed to make all this happen:

  1. Set the Visible property of rptMasterDetail to False.

  2. Switch to the code module for the Main Form, and add the name of the code module for the Report form in a Uses statement in the Implementation section of the code:


uses rpt_masterdetail;

  1. Finally, add a Button to the Main Form. Set its Caption to Preview, and add the following code line to the button’s OnClick event:


or, set the Caption to Print, and put this code in the OnClick event to print directly to the default printer:


Congratulations! Save, compile and run the application, and you should get a preview or printout that matches the screen shot at the start of the Tutorial.