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:
- Create a new C++ Builder VCL Forms Application.
- Drag a TADOConnection component called ADOConnection1 onto the form. Set its LoginPrompt value to "False." Under the ConnectionStrings property, select "Use Data Link File" and click "Browse." Navigate to the BCDEMOS.udl file (which should be in the "Common Files" folder).
- Drag a TADODataSet component called ADODataSet1 onto the form. Set its Connection to "ADOConnection1." Set its CommandText property to "select * from employee;" and set its "Connected" property to True. This should create a solid connection to the sample database.
- Drag a TButton called Button1 onto the form. Double-click the button to initiate an OnClick event handler.
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.