Basic Tutorial Part 1

By Stephen Frye

This article is Part 1 of a 2-part tutorial on building reports in Lazarus using FortesReport. It will follow the basic structure of the FortesReport Part 1 tutorial found on the Prof. Carlos website, but with two differences: it is written natively in English, and it will exclusively use a Firebird database for data storage.

Part 1 will build a single-table report with Grouping. Part 2 will build a master-detail report. Since FortesReport is similar in concept to QuickReport, familiarity with QuickReport will help a great deal.

Before Starting

If you haven’t done so already, there are three things to do before continuing with this tutorial:

  1. Download and install the Firebird database engine. Both tutorials will use one of the databases included in this download.
  2. Download and install into Lazarus the ZeosLib database-connection component package.
  3. (Obviously) Download and install into Lazarus the FortesReport for Lazarus package.

If you’re not familiar with installing packages into Lazarus, instructions can be found here.

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:

Connect to the Database

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.

Again from Zeos Access, place a TZReadOnlyQuery component in the DataModule, and set the following properties:

Test this by right-clicking qryEmployees, 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.

Finally, 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 the Employees in the Firebird database, along with their Job Title, where they work, and their phone number. To make it useful, we’ll give the report a Page Header and Column Header along with the detail information, and group the report by the first letter of the Last Name (surname). When done it should look something like this:

Bring frmRpt into view, and place a TRLReport component onto the form from the FortesReport tab. Give it a meaningful name in the Object Inspector, for example “rptEmployees”, and set the DataSource property to dmRpt.dsEmpList.

This component takes the role of a “virtual sheet of paper” where you’ll lay out the report elements as you want them to appear. This tutorial uses a Letter-size, landscape format, so expand the PageSetup property of the report component and set Orientation to poLandscape and the PaperSize to fpLetter. FortesReport appears to have been designed specifically using the Metric system for dimensions; all dimensions are set by default in millimeters, and I haven’t yet discovered if there’s a way to change that. You may also find it helpful to expand the size of your report-design form so that you can see the entire sheet of paper.

FortesReport is a band-based report designer, so next we need to specify which type of report bands will be used with this report. 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:

If you haven’t figured it out already, the vertical height of each band in your report sets the amount of vertical space between each section and detail line when the report is previewed or printed..

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:

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 frmRpt 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:

implementation

uses frm_rpt;


  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:

frmRpt.rptEmployees.Preview;


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

frmRpt.rptEmployees.Print;


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.