Reputation: 475
I am using Embarcadero RAD Studio XE5 with C++ Builder and FastReport 4.13.2. I create a FastReport with a TfrxFooter band that holds an aggregate "sum" function adding up the "Salary" field of all rows in the employee.db table of the pre-installed BCDEMOS sample database. This is a very simple summation function, but it keeps throwing an access violation when trying to display the report.
To reproduce this problem in Embarcadero RAD Studio XE5:
In the OnClick event handler, paste the following code:
TfrxReport* frxReport1 = new TfrxReport(NULL);
frxReport1->Clear();
TfrxDBDataset* frxDBDataset1 = new TfrxDBDataset(NULL);
frxReport1->DataSets->Add(frxDBDataset1);
frxDBDataset1->DataSet = ADODataSet1;
// UserName is required for aggregate functions
frxDBDataset1->UserName = "ADODataSet1";
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;
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;
TfrxMemoView* mField;
for (int i = 0; i < DataBand->DataSet->FieldsCount(); ++i)
{
const String fieldname = ADODataSet1->Fields->Fields[i]->FieldName;
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;
}
// 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;
// Create a summation function that displays
// the grand total of every employee's salary
// THIS LINE CAUSES THE ACCESS VIOLATION (RUNTIME ERROR)
GrandTotalSalary->Text = "Grand Total Salary: [Sum(<ADODataSet1.'Salary'>,MyDataBand,1)]";
frxReport1->ShowReport(true);
delete frxDBDataset1;
delete frxReport1;
return;
After converting this project to Delphi, I was able to get the program to run successfully, and I can confirm the correct syntax in Delphi is: GrandTotalSalary.Text := 'Grand Total Salary: [Sum(<ADODataSet1."Salary">,MyDataBand,1)]';
but I need this to work in C++Builder. I believe the problem is a simple syntax error with the formula, but I don't know what the correct C++ syntax is.
Upvotes: 3
Views: 6687
Reputation: 475
Go to Project --> Options --> Packages --> Runtime Packages. Set "Link with Runtime Packages" to False.
The default for this option is True for C++Builder, False for Delphi. This explains why the equivalent code worked in Delphi, but didn't work in C++Builder.
Upvotes: 1
Reputation: 2843
I can't find the BCDEMOS.udl file on my computer, so I can't reproduce your problem. However, I think the problem is that FastReports will consider the scripting to be in Delphi unless another language is specified. The scripting language within the report is, of course, independent of the language you are using for your program.
Thus, unless you are specifically changing the scripting language for your report (and there is no reason to unless you otherwise want to use C++ for scripting), the syntax for the formula for Grand Total Salary will be the same regardless of whether you are using Delphi or C++ Builder for your application.
Also, carefully watch the difference between single and double quotes. That has tripped me up a couple of times in FastReport scripting. Whether to use single or double is determined by the scripting language chosen.ot
Upvotes: 1