Coding a Report

A report layout is composed of a set of components. Like other standard Delphi components, the components that make up a report layout have a run-time interface. That is, they can be created and configured using Object Pascal code. This capability can be used to create entire reports dynamically. Take these steps to create a report dynamically:

1 Create data access components.

2 Create the report.

3 Create the report bands.

4 Add data-aware components.

5 Add formatting components.

These are the same steps you would follow to create a report using the Delphi IDE and the Report-Builder Report Designer. Creating the components manually requires some additional knowledge of how the different parts of the report fit together. The following discussion steps you through the actual code necessary to build a simple report.

1 Declare the necessary uses clause.

DB, {contains TDataSource}

DBTables, {contains TTable} ppReport, {contains TppReport} ppDBBDE, {contains TppBDEPipeline} ppBands, {contains all band classes} ppCtrls, {contains standard components} ppTypes, {contains all ReportBuilder enumerated types} ppVar; {contains SystemVariable and

Variable classes}

The types that are being used from each unit are documented in the comments of this code.

2 Declare the local variables necessary to create the report.

procedure TForm1.Button1Click(Sender:

var lTable: TTable; lDataSource: TDataSource; lDataPipeline: TppBDEPipeline; lReport: TppReport; lLabel1: TppLabel; lLabel2: TppLabel; lDBText1: TppDBText; lDBText2: TppDBText; lSysVar: TppSystemVariable;

The prefix 'l' stands for local. This coding standard makes local variables easily distinguishable from component level variables (which are prefixed with an 'F') and from proper component names (such as 'Report1').

3 Create data access components.

lTable := TTable.Create(Self); lTable.Name := 'tblCustomer'; lTable.DatabaseName := 'DBDemos'; lTable.TableName := 'customer.db'; lDataSource := TDataSource.Create(Self); lDataSource.Name := 'dsCustomer'; lDataSource.DataSet := lTable; lDataPipeline := TppBDEPipeline.Create. (Self);

lDataPipeline.Name := 'plCustomer'; lDataPipeline.DataSource := lDataSource;

These are standard Delphi data access components. It is not necessary to assign the Name property of these components; we've done this to give you an idea of how they would be named if they were created within the Delphi IDE.

uses

4 Create the report.

lReport := TppReport.Create(Self); lReport.DataPipeline := lDataPipeline;

The data pipeline assignment is vital here. Without a data pipeline assigned, this report would generate an endless number of pages.

5 Create the report bands.

lReport.CreateDefaultBands;

This method creates a header band, a detail band, and a footer band. It is also possible to create the bands individually and assign them to the report. A report must always have a detail band in order to generate properly.

6 Add labels to the header band.

lLabel1 := TppLabel.Create(Self); lLabel1.Band := lReport.HeaderBand; lLabel1.spLeft := 2; lLabel1.spTop := 2; lLabel1.Caption := 'Customer No.'; lLabel2 := TppLabel.Create(Self); lLabel2.Band := lReport.HeaderBand; lLabel2.spLeft := lLabel1.spLeft + lLabel1.spWidth + 3; lLabel2.spTop := 2; lLabel2.Caption := 'Company Name';

These two labels will print at the top of each page. The 'sp' in the spLeft and spTop properties refers to screen pixels. All positional properties of the components within a report are expressed in the units of the report itself (i.e. the value of the Report.Units property). Using the screen pixel version of these properties allows us to size and position components without concern for the current value of the Unit property.

7 Add data-aware components to the detail band.

lDBText1 := TppDBText.Create(Self); lDBText1.Band := lReport.DetailBand; lDBText1.spLeft := lLabel1.spLeft; lDBText1.spTop := lLabel1.spTop; lDBText1.DataPipeline := lDataPipeline; lDBText1.DataField := 'CustNo'; lDBText2 := TppDBText.Create(Self); lDBText2.Band := lReport.DetailBand; lDBText2.spLeft := lLabel2.spLeft; lDBText2.spTop := lLabel2.spTop; lDBText2.DataPipeline := lDataPipeline; lDBText2.DataField := 'Company';

These components are positioned in the detail band directly below their corresponding label components.

8 Add a page number and timestamp to the footer band.

lSysVar := TppSystemVariable.Create ^ (Self);

lSysVar.Band := lReport.FooterBand; lSysVar.VarType := vtPrintDateTime; lSysVar.spLeft := 2; lSysVar.spTop := 2;

lSysVar := TppSystemVariable.Create. ^ (Self);

lSysVar.Band := lReport.FooterBand; lSysVar.VarType := vtPageNoDesc; lSysVar.Alignment := taRightJustify; lSysVar.spLeft :=

(lReport.PrinterSetup.PageDef.spPrintableWidth ^ -lSysVar.spWidth) -2; lSysVar.spTop := 2;

Notice how the second system variable component is right-justified. The AutoSize property defaults to True for system variables, so we need only to set the Alignment property and component position. Here we set spLeft so that the component is flush with the right margin of the page (less 2 pixels for spacing). Because it is right-justified, this component will expand to the right when it prints. The PrinterSetup.PageDef object contains all of the dimensions of the page. The spPrintableWidth property contains the width of the paper less the left and right margins (in screen pixels).

9 Preview the report.

lReport.Print;

The DeviceType property of a report defaults to 'Screen,' so calling print here causes the Print Preview form to be displayed.

10 Free the report.

lReport.Free;

The ModalPreview property of the report defaults to True; therefore, this line of code will only fire after the Print Preview form has been closed. When the report is freed, it will automatically free the bands and components it contains.

11 Free the data access components.

lTable.Free ; lDataSource.Free; lDataPipeline.Free;

The data access components are not freed by the report, and so must be freed separately.

Was this article helpful?

+1 0
Project Management Made Easy

Project Management Made Easy

What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.

Get My Free Ebook


Responses

  • emma
    Is it possible to assign a variable value in report builder in delphi?
    2 years ago
  • Geraldino Barese
    How to use stringreplace function in report builder in delpi?
    12 months ago
  • valter
    Is report Builder written in Delphi?
    1 month ago

Post a comment