Creating and Printing a Report

The LazReport component is not installed by default. You need to recompile Lazarus so that it appears on the toolbar. Select menu item Packages > Install/Uninstall Packages, select lazreport in the "Do not install" list, click on the Install Selection button which is now activated, then click on the Save and rebuild IDE button. (We experienced error messages and resorted to selecting the menu item Tools > Build lazarus with profile: Clean Up + Build, which worked).

The documentation is Report Generator FreeReport v2.32 for Lazarus, which is part of the Lazarus installation and has the pathname lazarus\components\lazreport\doc\fr_eng.sxw. You need to read the relevant sections carefully. For example, the valuable information that (within a band) CTRL+Enter brings up the script editor is not prominent. When you make changes to your report, preview them regularly and keep saving good copies of the report to fall back on in case things go wrong. It may help to use the compiler directive {$mode delphi} instead of {$mode objfpc}{$H+} since the software was designed originally for use with Delphi.

We use the reporting tool to produce a master-detail report of programmers (master) and their programs (detail). Program FirebirdDemo3 uses queries, data sources, a transaction and a connection "hooked up" as usual. In addition, it uses a report, designer, preview and two DBDatasets (with types TfrReport, TfrDesigner, TfrPreview and TfrDBDataSet). Each DBDataset has its Dataset property set to the corresponding query and the report has its Dataset property set to one of the DBDatasets.

The screenshot shows the master-detail relationship, conditional formatting, and changes to the font face, colour and style.

Program FirebirdDemo3 in action

Program FirebirdDemo3 in action

You can see at the end of this page the contents of the XML file that is used in the generation of the report.

Type CTRL+Enter in the Detail data band of the designer to see this text in the Script section of the Text Editor

if [sqlqMaster."ID"] = [sqlqDetail."PROGRAMMERID"] then DetailData1.Visible := True
  else  DetailData1.Visible := False;

Within the Highlight tag of the XML file the colour is set with <FontColor Value="clBlue"/> then <HighlightStr Value="Value = 'Educational'"/> sets the string to be matched for conditional formatting.

Program FirebirdDemo3 enables you to design, preview and print the reports. You may like to incorporate graphics into your own reports.

We supply the source for all demonstration programs and the database Contributions.fdb in db_contributions.zip. The Firebird demonstrations are set up with the database in the F:\Firebird directory, so put it there or change the DatabaseName property of the connection to its filepath on your system. The demonstrations need either Firebird server to be running or the embedded Firebird files (fbclient.dll etc.) to be in your program folder. You can download the executable FirebirdDemo3.exe in db_firebird_demo3.zip if you are unable to compile our source files.

uFirebirdDemo3.pas

unit uFirebirdDemo3;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, sqldb, dbf, db, IBConnection, FileUtil, Forms, Controls,
  Graphics, Dialogs, StdCtrls, LR_Class, LR_DBSet, LR_Desgn, LR_View;

type
  TfrmReport = class(TForm)
    btnDesign: TButton;
    btnPrint: TButton;
    btnPreview: TButton;
    dsMaster: TDatasource;
    dsDetail: TDatasource;
    frdbdsMaster: TfrDBDataSet;
    frdbdsDetail: TfrDBDataSet;
    frDesigner1: TfrDesigner;
    frPreview1: TfrPreview;
    frReport1: TfrReport;
    IBConnection1: TIBConnection;
    sqlqMaster: TSQLQuery;
    sqlqDetail: TSQLQuery;
    SQLTransaction1: TSQLTransaction;
    procedure btnDesignClick(Sender: TObject);
    procedure btnPrintClick(Sender: TObject);
    procedure btnPreviewClick(Sender: TObject);
  end; 

var
  frmReport: TfrmReport;

implementation

{$R *.lfm}

procedure TfrmReport.btnDesignClick(Sender: TObject);
begin
  frReport1.LoadFromFile(ExtractFilePath(ParamStr(0)) + 'MasterDetail.lrf');
  frReport1.DesignReport;
end;

procedure TfrmReport.btnPrintClick(Sender: TObject);
begin
  frReport1.LoadFromFile(ExtractFilePath(ParamStr(0)) + 'MasterDetail.lrf');
  frReport1.PrepareReport;
  frReport1.PrintPreparedReport('',1);
end;

procedure TfrmReport.btnPreviewClick(Sender: TObject);
begin
  frReport1.LoadFromFile(ExtractFilePath(ParamStr(0)) + 'MasterDetail.lrf');
  frReport1.ShowReport;
end;

end.

uFirebirdDemo3.lfm

object frmReport: TfrmReport
  Left = 328
  Height = 580
  Top = 168
  Width = 808
  Caption = 'Report'
  ClientHeight = 580
  ClientWidth = 808
  LCLVersion = '0.9.30'
  object frPreview1: TfrPreview
    Left = 8
    Height = 529
    Top = 0
    Width = 776
    BevelOuter = bvLowered
    Caption = 'frPreview1'
    TabOrder = 0
    ScrollBars = ssBoth
  end
  object btnDesign: TButton
    Left = 232
    Height = 25
    Top = 536
    Width = 75
    Caption = 'Design'
    OnClick = btnDesignClick
    TabOrder = 1
  end
  object btnPrint: TButton
    Left = 472
    Height = 25
    Top = 536
    Width = 75
    Caption = 'Print'
    OnClick = btnPrintClick
    TabOrder = 2
  end
  object btnPreview: TButton
    Left = 352
    Height = 25
    Top = 536
    Width = 75
    Caption = 'Preview'
    OnClick = btnPreviewClick
    TabOrder = 3
  end
  object sqlqMaster: TSQLQuery
    IndexName = 'DEFAULT_ORDER'
    Active = True
    Database = IBConnection1
    Transaction = SQLTransaction1
    ReadOnly = False
    SQL.Strings = (
      'Select * from Programmer order by ForeName, Surname'
    )
    Params = <>
    left = 26
  end
  object IBConnection1: TIBConnection
    Connected = True
    LoginPrompt = False
    DatabaseName = 'F:\Firebird\CONTRIBUTIONS.FDB'
    KeepConnection = False
    Password = 'pp4s'
    Transaction = SQLTransaction1
    UserName = 'student'
    left = 427
  end
  object frReport1: TfrReport
    Dataset = frdbdsMaster
    InitialZoom = pzDefault
    Options = []
    Preview = frPreview1
    PreviewButtons = [pbZoom, pbLoad, pbSave, pbPrint, pbFind, pbHelp, pbExit]
    DataType = dtDataSet
    left = 514
    ReportForm = {
      19000000
    }
  end
  object frdbdsMaster: TfrDBDataSet
    DataSet = sqlqMaster
    left = 595
  end
  object frDesigner1: TfrDesigner
    left = 688
    top = 64
  end
  object sqlqDetail: TSQLQuery
    IndexName = 'DEFAULT_ORDER'
    Active = True
    Database = IBConnection1
    Transaction = SQLTransaction1
    ReadOnly = False
    SQL.Strings = (
      'select * from programs order by Name'
    )
    Params = <>
    left = 100
  end
  object dsMaster: TDatasource
    DataSet = sqlqMaster
    left = 168
  end
  object dsDetail: TDatasource
    DataSet = sqlqDetail
    left = 232
  end
  object SQLTransaction1: TSQLTransaction
    Active = True
    Action = caNone
    Database = IBConnection1
    left = 321
  end
  object frdbdsDetail: TfrDBDataSet
    DataSet = sqlqDetail
    left = 686
  end
end

FirebirdDemo3.lpr

program FirebirdDemo3;

{$mode objfpc}{$H+}

uses
  Interfaces, Forms, lazreport, dbflaz, uFirebirdDemo3;

{$R *.res}

begin
  Application.Initialize;
  Application.CreateForm(TfrmReport, frmReport);
  Application.Run;
end.  
      

MasterDetail.lrf

<?xml version="1.0"?>
<CONFIG>
  <LazReport>
    <Version Value="25"/>
    <Title Value=""/>
    <Subject Value=""/>
    <KeyWords Value=""/>
    <Comments Value=""/>
    <ReportVersionBuild Value=""/>
    <ReportVersionMajor Value=""/>
    <ReportVersionMinor Value=""/>
    <ReportVersionRelease Value=""/>
    <ReportAutor Value=""/>
    <Pages>
      <PrintToDefault Value="False"/>
      <DoublePass Value="False"/>
      <SelectedPrinter Value="Brother HL-2030 series"/>
      <PageCount Value="1"/>
      <Page1>
        <Name Value="Page1"/>
        <ClassName Value="TfrPageReport"/>
        <Visible Value="1"/>
        <Width Value="1"/>
        <Height Value="1" PageType="0"/>
        <PgSize Value="9"/>
        <Margins>
          <left Value="0"/>
          <Top Value="0"/>
          <Right Value="0"/>
          <Bottom Value="0"/>
        </Margins>
        <Orientation Value="poPortrait"/>
        <UseMargins Value="True"/>
        <PrintToPrevPage Value="False"/>
        <ColCount Value="1"/>
        <ColGap Value="0"/>
        <LayoutOrder Value="loColumns"/>
        <ObjectCount Value="8"/>
        <Object1>
          <Name Value="MasterData1"/>
          <ClassName Value="TfrBandView"/>
          <Visible Value="1"/>
          <Typ Value="gtBand"/>
          <StreamMode Value="0"/>
          <Size>
            <Left Value="0"/>
            <Top Value="75"/>
            <Width Value="752"/>
            <Height Value="28"/>
          </Size>
          <Flags Value="48"/>
          <Data>
            <Script Value="FullName:= [sqlqMaster.&quot;FORENAME&quot;] + ' ' + [sqlqMaster.&quot;SURNAME&quot;]&#xD;&#xA;"/>
          </Data>
          <BandType Value="btMasterData"/>
          <Condition Value=""/>
          <DatasetStr Value="frdbdsMaster"/>
        </Object1>
        <Object2>
          <Name Value="DetailData1"/>
          <ClassName Value="TfrBandView"/>
          <Visible Value="1"/>
          <Typ Value="gtBand"/>
          <StreamMode Value="0"/>
          <Size>
            <Left Value="0"/>
            <Top Value="119"/>
            <Width Value="752"/>
            <Height Value="40"/>
          </Size>
          <Flags Value="48"/>
          <Data>
            <Script Value="if [sqlqMaster.&quot;ID&quot;] = [sqlqDetail.&quot;PROGRAMMERID&quot;] then DetailData1.Visible := True&#xD;&#xA; else  DetailData1.Visible := False;&#xD;&#xA;"/>
          </Data>
          <BandType Value="btDetailData"/>
          <Condition Value=""/>
          <DatasetStr Value="frdbdsDetail"/>
        </Object2>
        <Object3>
          <Name Value="ReportTitle1"/>
          <ClassName Value="TfrBandView"/>
          <Visible Value="1"/>
          <Typ Value="gtBand"/>
          <StreamMode Value="0"/>
          <Size>
            <Left Value="0"/>
            <Top Value="16"/>
            <Width Value="752"/>
            <Height Value="40"/>
          </Size>
          <Flags Value="48"/>
          <Data>
            <Script Value=""/>
          </Data>
          <BandType Value="btReportTitle"/>
          <Condition Value=""/>
          <DatasetStr Value=""/>
        </Object3>
        <Object4>
          <Name Value="Memo2"/>
          <ClassName Value="TfrMemoView"/>
          <Visible Value="1"/>
          <Typ Value="gtMemo"/>
          <StreamMode Value="0"/>
          <Size>
            <Left Value="121"/>
            <Top Value="120"/>
            <Width Value="200"/>
            <Height Value="18"/>
          </Size>
          <Flags Value="3"/>
          <FillColor Value="clNone"/>
          <Frames>
            <FrameColor Value="clBlack"/>
            <FrameStyle Value="frsSolid"/>
            <FrameWidth Value="1"/>
            <FrameBorders Value=""/>
          </Frames>
          <Data>
            <Format Value="558"/>
            <FormatStr Value=""/>
            <Memo Value="[sqlqDetail.&quot;NAME&quot;]&#xD;&#xA;"/>
            <Script Value=""/>
          </Data>
          <Font>
            <Name Value="Courier New"/>
            <Size Value="10"/>
            <Color Value="clBlack"/>
            <Charset Value="0"/>
            <Style Value=""/>
          </Font>
          <Highlight>
            <FontStyle Value="2"/>
            <FontColor Value="clBlack"/>
            <FillColor Value="clWhite"/>
            <HighlightStr Value=""/>
          </Highlight>
          <Alignment Value="taLeftJustify"/>
          <Layout Value="tlTop"/>
          <Angle Value="0"/>
        </Object4>
        <Object5>
          <Name Value="Memo4"/>
          <ClassName Value="TfrMemoView"/>
          <Visible Value="1"/>
          <Typ Value="gtMemo"/>
          <StreamMode Value="0"/>
          <Size>
            <Left Value="273"/>
            <Top Value="120"/>
            <Width Value="220"/>
            <Height Value="34"/>
          </Size>
          <Flags Value="3"/>
          <FillColor Value="clNone"/>
          <Frames>
            <FrameColor Value="clBlack"/>
            <FrameStyle Value="frsSolid"/>
            <FrameWidth Value="1"/>
            <FrameBorders Value=""/>
          </Frames>
          <Data>
            <Format Value="558"/>
            <FormatStr Value=""/>
            <Memo Value="[sqlqDetail.&quot;DESCRIPTION&quot;]&#xD;&#xA;"/>
            <Script Value=""/>
          </Data>
          <Font>
            <Name Value="Arial"/>
            <Size Value="10"/>
            <Color Value="clBlack"/>
            <Charset Value="0"/>
            <Style Value=""/>
          </Font>
          <Highlight>
            <FontStyle Value="2"/>
            <FontColor Value="clBlack"/>
            <FillColor Value="clWhite"/>
            <HighlightStr Value=""/>
          </Highlight>
          <Alignment Value="taLeftJustify"/>
          <Layout Value="tlTop"/>
          <Angle Value="0"/>
        </Object5>
        <Object6>
          <Name Value="Memo5"/>
          <ClassName Value="TfrMemoView"/>
          <Visible Value="1"/>
          <Typ Value="gtMemo"/>
          <StreamMode Value="0"/>
          <Size>
            <Left Value="521"/>
            <Top Value="120"/>
            <Width Value="200"/>
            <Height Value="18"/>
          </Size>
          <Flags Value="3"/>
          <FillColor Value="clNone"/>
          <Frames>
            <FrameColor Value="clBlack"/>
            <FrameStyle Value="frsSolid"/>
            <FrameWidth Value="1"/>
            <FrameBorders Value=""/>
          </Frames>
          <Data>
            <Format Value="558"/>
            <FormatStr Value=""/>
            <Memo Value="[sqlqDetail.&quot;CATEGORY&quot;]&#xD;&#xA;"/>
            <Script Value=""/>
          </Data>
          <Font>
            <Name Value="Arial"/>
            <Size Value="10"/>
            <Color Value="clBlack"/>
            <Charset Value="0"/>
            <Style Value=""/>
          </Font>
          <Highlight>
            <FontStyle Value="0"/>
            <FontColor Value="clBlue"/>
            <FillColor Value="clWhite"/>
            <HighlightStr Value="Value = 'Educational'"/>
          </Highlight>
          <Alignment Value="taLeftJustify"/>
          <Layout Value="tlTop"/>
          <Angle Value="0"/>
        </Object6>
        <Object7>
          <Name Value="Memo6"/>
          <ClassName Value="TfrMemoView"/>
          <Visible Value="1"/>
          <Typ Value="gtMemo"/>
          <StreamMode Value="0"/>
          <Size>
            <Left Value="40"/>
            <Top Value="79"/>
            <Width Value="140"/>
            <Height Value="18"/>
          </Size>
          <Flags Value="3"/>
          <FillColor Value="clNone"/>
          <Frames>
            <FrameColor Value="clBlack"/>
            <FrameStyle Value="frsSolid"/>
            <FrameWidth Value="1"/>
            <FrameBorders Value=""/>
          </Frames>
          <Data>
            <Format Value="558"/>
            <FormatStr Value=""/>
            <Memo Value="[Fullname]&#xD;&#xA;"/>
            <Script Value="Fullname := [sqlqMaster.&quot;FORENAME&quot;] + ' ' + [sqlqMaster.&quot;SURNAME&quot;]&#xD;&#xA;"/>
          </Data>
          <Font>
            <Name Value="Arial"/>
            <Size Value="10"/>
            <Color Value="clBlack"/>
            <Charset Value="0"/>
            <Style Value="fsBold"/>
          </Font>
          <Highlight>
            <FontStyle Value="2"/>
            <FontColor Value="clBlack"/>
            <FillColor Value="clWhite"/>
            <HighlightStr Value=""/>
          </Highlight>
          <Alignment Value="taLeftJustify"/>
          <Layout Value="tlTop"/>
          <Angle Value="0"/>
        </Object7>
        <Object8>
          <Name Value="Memo1"/>
          <ClassName Value="TfrMemoView"/>
          <Visible Value="1"/>
          <Typ Value="gtMemo"/>
          <StreamMode Value="0"/>
          <Size>
            <Left Value="328"/>
            <Top Value="28"/>
            <Width Value="120"/>
            <Height Value="18"/>
          </Size>
          <Flags Value="3"/>
          <FillColor Value="clNone"/>
          <Frames>
            <FrameColor Value="clBlack"/>
            <FrameStyle Value="frsSolid"/>
            <FrameWidth Value="1"/>
            <FrameBorders Value=""/>
          </Frames>
          <Data>
            <Format Value="558"/>
            <FormatStr Value=""/>
            <Memo Value="Contributions&#xD;&#xA;"/>
            <Script Value=""/>
          </Data>
          <Font>
            <Name Value="Arial"/>
            <Size Value="12"/>
            <Color Value="clBlack"/>
            <Charset Value="0"/>
            <Style Value="fsBold"/>
          </Font>
          <Highlight>
            <FontStyle Value="2"/>
            <FontColor Value="clBlack"/>
            <FillColor Value="clWhite"/>
            <HighlightStr Value=""/>
          </Highlight>
          <Alignment Value="taCenter"/>
          <Layout Value="tlTop"/>
          <Angle Value="0"/>
        </Object8>
      </Page1>
      <FVal>
        <Count Value="0"/>
      </FVal>
      <ParentVars Value="FullName&#xD;&#xA;"/>
    </Pages>
  </LazReport>
</CONFIG>
Programming - a skill for life!

Creating, editing, and searching a Firebird database, printing a report, stored procedures and ClientDataSets