Cannot get "Sum" aggregate function working. Please help!

edited 12:25PM in FastReport 4.0
I am trying to add an aggregate "sum" function into my fastreport, and I don't quite have the syntax correct. This is a simple example (built off of the example in the Programmer's Manual, found here: http://www.fast-report.com/public_download...m_from_code.htm ) using Embarcadero RAD Studio XE5/C++Builder that creates a report of a basic table as its dataset. Everything's working good, except my GrandTotalSalary Text property is incorrect. My code is as follows:
try
{
  ADODataSet1->Open();
}
catch(Exception& e)
{
  LogIt("ERROR: Could not open ADODataSet1: '" + e.Message + "'");
  return;
}

TfrxReport* frxReport1 = new TfrxReport(NULL);
frxReport1->Clear();

TfrxDBDataset* frxDBDataset1 = new TfrxDBDataset(NULL);
frxReport1->DataSets->Add(frxDBDataset1);
frxDBDataset1->DataSet = ADODataSet1;

frxDBDataset1->UserName = "ADODataSet1";
LogIt("frxDBDataset's name: '" + frxDBDataset1->DataSet->Name + "'");

TfrxDataPage* DataPage = new TfrxDataPage(frxReport1);
DataPage->CreateUniqueName();

TfrxReportPage* Page = new TfrxReportPage(frxReport1);

Page->CreateUniqueName();

// set sizes of fields, paper and orientation to defaults
Page->SetDefaults();
Page->Orientation = poPortrait;

TfrxReportTitle* HeaderBand = new TfrxReportTitle(Page);
HeaderBand->CreateUniqueName();

HeaderBand->Top = 0;
HeaderBand->Height = 20;

LogIt("HeaderBand Top: '" + String(int(HeaderBand->Top)) + "'");

TfrxMemoView* Memo = new TfrxMemoView(HeaderBand);
Memo->CreateUniqueName();
Memo->Text = "Report of Employee Table";
Memo->SetBounds(0, 0, 200, 20);

TfrxHeader* ColumnHeaderBand;
ColumnHeaderBand = new TfrxHeader(Page);
ColumnHeaderBand->CreateUniqueName();
ColumnHeaderBand->Top = HeaderBand->Top + HeaderBand->Height;
ColumnHeaderBand->Height = 20;

TfrxMasterData* DataBand = new TfrxMasterData(Page);
DataBand->Name = "MyDataBand";
DataBand->DataSet = frxDBDataset1;
DataBand->Top = ColumnHeaderBand->Top + ColumnHeaderBand->Height;
DataBand->Height = 20;

LogIt("DataBand Top: '" + String(int(DataBand->Top)) + "'");

TfrxMemoView* mField;
for (int i = 0; i < DataBand->DataSet->FieldsCount(); ++i)
{
  const String fieldname = ADODataSet1->Fields->Fields[i]->FieldName;
  LogIt("Current field: '" + fieldname + "'");

  // Take care of the field names in the column header first
  mField = new TfrxMemoView(ColumnHeaderBand);
  mField->CreateUniqueName();
  mField->SetBounds(i * 100, 0, 100, 20);
  mField->Text = fieldname;
  mField->HAlign = haCenter;

  // Now do the actual data
  mField = new TfrxMemoView(DataBand);
  mField->CreateUniqueName();
  mField->DataSet = DataBand->DataSet;
  mField->DataField = fieldname;
  mField->SetBounds(i * 100, 0, 100, 20);
  mField->HAlign = haRight;
} // end iterate through DataBand

// Now do footer band. This will hold the grand total salary amount
TfrxBand* FooterBand = new TfrxFooter(Page);
FooterBand->CreateUniqueName();
FooterBand->Top = DataBand->Top + DataBand->Height;
FooterBand->Height = HeaderBand->Height;

TfrxMemoView* GrandTotalSalary = new TfrxMemoView(FooterBand);
GrandTotalSalary->Top = 0;
GrandTotalSalary->Left = 0;
GrandTotalSalary->Height = 20;
GrandTotalSalary->Align = baWidth;

// Display the grand total of every employee's salary
// ERROR: Causes Access Violation (runtime error)
GrandTotalSalary->Text = "Grand Total Salary: [Sum(<ADODataSet1.\"Salary\">,MyDataBand)]";
LogIt("GrandTotalSalary aggregate formula: " + GrandTotalSalary->Text);

// Save report
String path = "C:\\temp\\fastreportdemo.fr3";
frxReport1->SaveToFile(path);
//LogIt("File saved to: " + path);

// Change the default zoom setting when you view the report
frxReport1->PreviewOptions->ZoomMode = zmPageWidth;

// show report
frxReport1->ShowReport(true);

delete frxDBDataset1;
delete frxReport1;

return;

As I understand it, in order to get these functions working you need two things:
1. You need to explicitly set the UserName property for the frxDBDataSet component (here we give it the name "ADODataSet1")
2. You need to explicitly set the Name property of the DataBand you are accessing (here it is "MyDataBand")

Both of these conditions are of course true. And when I view my logfile, I can see that the quotes in the "Salary" portion of the expression are getting escaped correctly, so I am completely stumped. Please tell me where my error is.

Comments

  • gordkgordk St.Catherines On. Canada.
    edited 12:25PM
    i am not a c programmer but here is the delphi equivalent

    GrandTotalSalary.Text = 'Grand Total Salary: ' +'[Sum(<ADODataSet1."Salary">,MyDataBand,1)]';
    note ADODataSet1 this must be the usename of the dataset
  • edited 12:25PM
    I am setting the "UserName" property in my code to the correct value, but is there something else I have to do? What is a "username" in this context, anyway?
  • edited 12:25PM
    gordk wrote: »
    i am not a c programmer but here is the delphi equivalent

    GrandTotalSalary.Text = 'Grand Total Salary: ' +'[Sum(<ADODataSet1."Salary">,MyDataBand,1)]';
    note ADODataSet1 this must be the usename of the dataset

    I rewrote the program in Delphi and was able to successfully compile and run the program. Therefore, the problem must be a syntactical difference between Delphi and C++, and not a FastReports problem. I have posted a message on a C++ Builder forum for help instead.

Leave a Comment

Rich Text Editor. To edit a paragraph's style, hit tab to get to the paragraph menu. From there you will be able to pick one style. Nothing defaults to paragraph. An inline formatting menu will show up when you select text. Hit tab to get into that menu. Some elements, such as rich link embeds, images, loading indicators, and error messages may get inserted into the editor. You may navigate to these using the arrow keys inside of the editor and delete them with the delete or backspace key.