Search Results for

    Show / Hide Table of Contents

    Creating a report form from code

    As a rule, you will create most reports using the designer. Nevertheless, in some cases (for example, when the report’s form is unknown) it is necessary to create a report manually, from code.

    To create a report manually, one should perform the following steps in order:

    • clear the report component

    • add data sources

    • add the "Data" page

    • add report’s page

    • add bands on a page

    • set bands’ properties, and then connect them to the data

    • add objects on each band

    • set objects’ properties, and then connect them to the data

    Let us examine creation of a simple report of the «list» type. Assume that we have the following components: frxReport1: TfrxReport and frxDBDataSet1: TfrxDBDataSet (the last one is connected to data from the DBDEMOS, the «Customer.db» table). Our report will contain one page with the «Report Title» and «Master Data» bands. On the «Report Title» band there will be an object with the "Hello FastReport!" text, and the «Master Data» one will contain an object with a link to the "CustNo" field.

    Pascal:

    var
      DataPage: TfrxDataPage;
      Page: TfrxReportPage;
      Band: TfrxBand;
      DataBand: TfrxMasterData;
      Memo: TfrxMemoView;
    
    { clear a report }
    frxReport1.Clear;
    
    { add a dataset to the list of ones accessible for a report }
    frxReport1.DataSets.Add(frxDBDataSet1);
    
    { add the "Data" page }
    DataPage := TfrxDataPage.Create(frxReport1);
    
    { add a page }
    Page := TfrxReportPage.Create(frxReport1);
    
    { create a unique name }
    Page.CreateUniqueName;
    
    { set sizes of fields, paper and orientation by default }
    Page.SetDefaults;
    
    { modify paper’s orientation }
    Page.Orientation := poLandscape;
    
    { add a report title band}
    Band := TfrxReportTitle.Create(Page);
    Band.CreateUniqueName;
    
    { it is sufficient to set the «Top» coordinate and height for a band }
    { both coordinates are in pixels }
    Band.Top := 0;
    Band.Height := 20;
    
    { add an object to the report title band }
    Memo := TfrxMemoView.Create(Band);
    Memo.CreateUniqueName;
    Memo.Text := 'Hello FastReport!';
    Memo.Height := 20;
    
    { this object will be stretched according to band’s width }
    Memo.Align := baWidth;
    
    { add the masterdata band }
    DataBand := TfrxMasterData.Create(Page);
    DataBand.CreateUniqueName;
    DataBand.DataSet := frxDBDataSet1;
    
    { the Top coordinate should be greater than the previously added band’s top + height}
    DataBand.Top := 100;
    DataBand.Height := 20;
    
    { add an object on master data }
    Memo := TfrxMemoView.Create(DataBand);
    Memo.CreateUniqueName;
    
    { connect to data }
    Memo.DataSet := frxDBDataSet1;
    Memo.DataField := 'CustNo';
    Memo.SetBounds(0, 0, 100, 20);
    
    { adjust the text to the right object’s margin }
    Memo.HAlign := haRight;
    
    { show the report }
    frxReport1.ShowReport;
    

    C++:

    TfrxDataPage * DataPage;
    TfrxReportPage * Page;
    TfrxBand * Band;
    TfrxMasterData * DataBand;
    TfrxMemoView * Memo;
    
    // clear a report 
    frxReport1->Clear();
    
    // add a dataset to the list of ones accessible for a report 
    frxReport1->DataSets->Add(frxDBDataset1);
    
    // add the "Data" page 
    DataPage = new TfrxDataPage(frxReport1);
    
    // add a page
    Page = new TfrxReportPage(frxReport1);
    
    // create a unique name 
    Page->CreateUniqueName();
    
    // set sizes of fields, paper and orientation by default 
    Page->SetDefaults();
    
    // modify paper’s orientation
    Page->Orientation = poLandscape;
    
    // add a report title band
    Band = new TfrxReportTitle(Page);
    Band->CreateUniqueName();
    
    // it is sufficient to set the «Top» coordinate and height for a band 
    // both coordinates are in pixels 
    Band->Top = 0;
    Band->Height = 20;
    
    // add an object to the report title band 
    Memo = new TfrxMemoView(Band);
    Memo->CreateUniqueName();
    Memo->Text = "Hello FastReport!";
    Memo->Height = 20;
    
    // this object will be stretched according to band’s width 
    Memo->Align = baWidth;
    
    // add the masterdata band 
    DataBand = new TfrxMasterData(Page);
    DataBand->CreateUniqueName();
    DataBand->DataSet = frxDBDataset1;
    
    // the Top coordinate should be greater than the previously added band’s top + height
    DataBand->Top = 100;
    DataBand->Height = 20;
    
    // add an object on master data 
    Memo = new TfrxMemoView(DataBand);
    Memo->CreateUniqueName();
    
    // connect to data 
    Memo->DataSet = frxDBDataset1;
    Memo->DataField = "CustNo";
    Memo->SetBounds(0, 0, 100, 20);
    
    // adjust the text to the right object’s margin 
    Memo->HAlign = haRight;
    
    // show the report 
    frxReport1->ShowReport(true);
    

    Let us explain some details.

    All the data sources, which are to be used in the report, must be added to the list of data sources. In our case, this is performed using the

    frxReport1.DataSets.Add(frxDBDataSet1)
    

    line. Otherwise, a report will not work.

    The "Data" page is necessary for inserting internal datasets such as TfrxADOTable into the report. Such datasets can be placed only to the "Data" page.

    The call for Page.SetDefaults is not necessary, since in this case a page will have the А4 format and margins of 0 mm. SetDefaults sets 10mm margins and takes page size and alignment, which a printers have by default.

    While adding bands to a page, you should make sure they do not overlap each other. To perform this, it is sufficient to set the «Top» and «Height» coordinates. There is no point in modifying the «Left» and «Width» coordinates, since a band always has the width of the page, on which it is located (in case of vertical bands it's not true – you should set Left and Width properties and don't care about Top and Height). One should note, that the order of bands’ location on a page is of great importance. Always locate bands in the same way you would do it in the designer.

    Objects’ coordinates and sizes are set in pixels. Since the Left, Top, Width, and Height properties of all objects have the «Extended» type, you can point out non-integer values. The following constants are defined for converting pixels into centimeters and inches:

    fr01cm = 3.77953;
    fr1cm  = 37.7953;
    fr01in = 9.6;
    fr1in  = 96;
    

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

    Band.Height := fr01cm * 5; 
    Band.Height := fr1cm * 0.5; 
    
    Back to top © Copyright Fast Reports Inc.