Creating a report form from code

Top  Previous  Next

As a rule you will create most reports using the designer. However, sometimes it is necessary to create a report manually in code, for example when the report’s form is unknown.

 

To create a report manually requires the following steps in order:

- clear the report component

- add data sources

- add the “Data” page

- add the report page

- add bands on the page

- set band properties and then connect them to the data

- add objects on each band

- set object properties and then connect them to the data

 

Let's look at the creation of a simple report of “list” type. Assume we have the following components: frxReport1: TfrxReport and frxDBDataSet1: TfrxDBDataSet (the latter connected to data from the DBDEMOS “Customer.db” table). Our report will contain one page with “ReportTitle” and “MasterData” bands. On the “ReportTitle” band there will be an object containing "Hello FastReport!" and the “MasterData” band will contain an object linked to the "CustNo" field.

 

Pascal:

 

var

 DataPage: TfrxDataPage;

 Page: TfrxReportPage;

 Band: TfrxBand;

 DataBand: TfrxMasterData;

 Memo: TfrxMemoView;

 

{ clear report }

frxReport1.Clear;

 

{ add dataset to list of datasets accessible in report }

frxReport1.DataSets.Add(frxDBDataSet1);

 

{ add "Data" page }

DataPage := TfrxDataPage.Create(frxReport1);

 

{ add page }

Page := TfrxReportPage.Create(frxReport1);

{ create a unique name }

Page.CreateUniqueName;

{ set sizes of fields, paper and orientation to defaults }

Page.SetDefaults;

{ change paper orientation }

Page.Orientation := poLandscape;

 

{ add report title band}

Band := TfrxReportTitle.Create(Page);

Band.CreateUniqueName;

{ only “Top” coordinate and height of band need setting

   both in pixels }

Band.Top := 0;

Band.Height := 20;

 

{ add object to report title band }

Memo := TfrxMemoView.Create(Band);

Memo.CreateUniqueName;

Memo.Text := 'Hello FastReport!';

Memo.Height := 20;

{ this object will be stretched to band’s width }

Memo.Align := baWidth;

 

{ add masterdata band }

DataBand := TfrxMasterData.Create(Page);

DataBand.CreateUniqueName;

DataBand.DataSet := frxDBDataSet1;

{ “Top” should be greater than previously added band’s top + height }

DataBand.Top := 100;

DataBand.Height := 20;

 

{ add object on masterdata }

Memo := TfrxMemoView.Create(DataBand);

Memo.CreateUniqueName;

{ connect to data }

Memo.DataSet := frxDBDataSet1;

Memo.DataField := 'CustNo';

Memo.SetBounds(0, 0, 100, 20);

{ align text to object’s right margin }

Memo.HAlign := haRight;

 

{ show report }

frxReport1.ShowReport;

 

C++:

 

TfrxDataPage * DataPage;

TfrxReportPage * Page;

TfrxBand * Band;

TfrxMasterData * DataBand;

TfrxMemoView * Memo;

 

// clear report

frxReport1->Clear();

 

// add dataset to list of datasets accessible in report

frxReport1->DataSets->Add(frxDBDataset1);

 

// add "Data" page

DataPage = new TfrxDataPage(frxReport1);

 

// add page

Page = new TfrxReportPage(frxReport1);

// create unique name

Page->CreateUniqueName();

// set sizes of fields, paper and orientation to defaults

Page->SetDefaults();

// change paper orientation

Page->Orientation = poLandscape;

 

// add report title band

Band = new TfrxReportTitle(Page);

Band->CreateUniqueName();

// only “Top” coordinate and height of band need setting

//    both in pixels

Band->Top = 0;

Band->Height = 20;

 

// add object to report title band

Memo = new TfrxMemoView(Band);

Memo->CreateUniqueName();

Memo->Text = "Hello FastReport!";

Memo->Height = 20;

// this object will be stretched to band’s width

Memo->Align = baWidth;

 

// add masterdata band

DataBand = new TfrxMasterData(Page);

DataBand->CreateUniqueName();

DataBand->DataSet = frxDBDataset1;

// “Top” should be greater than previously added band’s top + height

DataBand->Top = 100;

DataBand->Height = 20;

 

// add object on masterdata

Memo = new TfrxMemoView(DataBand);

Memo->CreateUniqueName();

// connect to data

Memo->DataSet = frxDBDataset1;

Memo->DataField = "CustNo";

Memo->SetBounds(0, 0, 100, 20);

// align text to object’s right margin

Memo->HAlign = haRight;

 

// show report

frxReport1->ShowReport(true);

 

Let's explain some details.

 

All the data sources that are to be used in the report must be added to the list of data sources, otherwise the report will not work. In our case use “frxReport1.DataSets.Add(frxDBDataSet1)”

 

The “Data” page is required when inserting internal datasets, such as “TfrxADOTable”, into a report. Such datasets can only be placed on the “Data” page.

 

The call to Page.SetDefaults is not essential since in this case the page will be А4 format with margins of 0 mm. SetDefaults sets margins to 10mm and page size and alignment to the default printer's values.

 

When adding bands to a page make sure that they do not overlap each other. To ensure this set the “Top” and “Height” properties. There is no point in changing the “Left” and “Width” properties since a band always has the same width as the page on which it is located : if the bands are vertical this is not so – instead set the “Left” and “Width” properties and don't bother with the “Top” and “Height” properties. Note that the ordering of the bands on the page is critical. Always locate bands in the same order as you would do in the designer.

 

Object coordinates and sizes are set in pixels. Since the “Left”, “Top”, “Width” and “Height” properties of all objects are of “Extended” type you can set non-integer values. The following constants are defined for converting pixels into centimeters or inches:

 

fr01cm = 3.77953;

fr1cm  = 37.7953;

fr01in = 9.6;

fr1in  = 96;

 

So for example, a band’s height can be set to 5 mm as follows:

 

Band.Height := fr01cm * 5;

Band.Height := fr1cm * 0.5;