Basic Tutorial Part 1
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:
- Download and install the Firebird database engine. Both tutorials will use one of the databases included in this download.
- Download and install into Lazarus the ZeosLib database-connection component package.
- (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:
- frmMain (frm_main.pas): “Main form” that will be the user-visible interface.
- frmRpt (frm_rpt.pas): form to hold the FortesReport components.
- dmRpt (dm_rpt.pas): the DataModule.
Connect to the Database
From the Zeos Access tab in the Lazarus IDE, place a TZConnection component into the DataModule. Set the following properties:
-
Database: find the “EMPLOYEES.FDB” database installed with the Firebird database engine.
-
Hostname: if EMPLOYEES.FDB is located on your desktop PC, enter “localhost”; otherwise use the IP address of the computer where the file is stored.
-
Name: (optional) give a relevant name to the connection. Used for this project: “dbEmployees”.
-
Password: masterkey.
-
Protocol: firebird-2.1 or whatever version you’re actually using.
-
User: sysdba.
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:
-
Connection: open the list and select dbEmployees.
-
Name: (optional) give a relevant name to the connection. Used for this project: “qryEmployees”.
-
SQL: SELECT
EMPLOYEE.EMP_NO,
EMPLOYEE.FIRST_NAME,
EMPLOYEE.LAST_NAME,
JOB.JOB_TITLE,
DEPARTMENT.DEPARTMENT,
DEPARTMENT.LOCATION,
EMPLOYEE.JOB_COUNTRY,
DEPARTMENT.PHONE_NO,
EMPLOYEE.PHONE_EXT
FROM
EMPLOYEE
INNER JOIN DEPARTMENT ON DEPARTMENT.MNGR_NO = EMPLOYEE.EMP_NO AND EMPLOYEE.DEPT_NO = DEPARTMENT.DEPT_NO
INNER JOIN JOB ON EMPLOYEE.JOB_CODE = JOB.JOB_CODE AND EMPLOYEE.JOB_GRADE = JOB.JOB_GRADE AND EMPLOYEE.JOB_COUNTRY = JOB.JOB_COUNTRY
ORDER BY
EMPLOYEE.LAST_NAME, EMPLOYEE.FIRST_NAME;
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:
-
DataSet: open the list and select qryEmployees.
-
Name: (optional) give a relevant name to the connection. Used for this project: “dsEmpList”.
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:
-
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.
-
Page Title: add a TRLLabel within the Page Header band. Set the following properties:
-
Align = faCenter to center the label within the band.
-
Alignment = taCenter to center-align the text within the label.
-
Caption = Employees Report.
-
Font = your desired font style and size.
-
-
System Information: we’ll add some controls in the Page Header to display the date when the report is printed, as well as the page number:
-
System Date: add a TRLSystemInfo control to the Page Header; manually place it on the left side of the page. Set Info to itDate, and set your desired font style and size.
-
Page Number: add a TRLSystemInfo control to the Page Header; manually place it on the right side of the page and set Alignment to taRightJustify. Set Info to itPageNumber, and set your desired font style and size.
-
-
-
Column Header: Place a new TRLBand underneath the Page Header band. Set its BandType to ColumnHeader and size it to allow room for your column labels.
-
Column Labels: Place a TRLLabel in the Column Header band for each data field that will be shown in the report. Set the Caption property of each label to the name you want displayed for each column.
-
-
Detail Items with Grouping: Now it’s time to add the detail items, and the Grouping of employees by Last Name. Place a TRLGroup band on the report underneath the Column Headers. Make it tall enough to contain two bands: a Group Header to display the first letter of the Surname for each group, and a Detail band to show the actual detail items. Set the following properties for the Group Band:
-
AllowedBands: Expand and set btDetail and btHeader to true.
-
Use either the DataFields or DataFormula property to define when to break. Selecting a field in DataFields will create a break whenever the value in that field changes. For this report, leave DataFields blank and set DataFormula to copy(LAST_NAME, 1, 1). This will calculate and create a break every time the first letter of the Last Name changes.
-
Group Header: Place a TRLBand in the upper part of the TRLGroup band. Set BandType to btHeader.
-
Place a TRLDBText component in the Group Header band. Set the font attributes as desired. Set the DataSource to dmRpt.dsEmpList. As with the TRLGroup band, set DataFormula to copy(LAST_NAME, 1, 1).
-
-
Detail Items: Place another TRLBand within the TRLGroup. Set BandType to btDetail
-
Place TRLDBText components in the Detail band for each item to be included in the report. Align them with the Column Headers previously placed. Set the DataSource for each data field to dmRpt.dsEmpList, and choose the correct DataField from the list of available fields.
-
Hint: You’ll save a little time and tedium by selecting all of the DBText components as a group (shift-click each one in turn to add it to the group), then set the DataSource once for the entire group.
-
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:
-
Set the Visible property of frmRpt to False.
-
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;
-
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.