Introduction to Rave Reports
Introduction to Rave Reports - Part IV: More Data Aware Reports
In Part III we have explored the data aware capabilities of Rave Reports, using the Driver Data View. On Part IV we are going to learn how to use Direct Data Views, the creation of Master/Detail reports, and the use of CalcOP and CalcTotal components to display calculated values and totals.
The Direct Data View
The Direct Data View allows you share the database connection established by your application, fetching data directly from the DataSets in your application. You can link them to any TDataSet descendant, and, with some code, you can even fetch data from custom DataStructures, like arrays of records or TStringLists. Apart from that, it behaves in the way as the Driver Data View we explored on Part III.
Using DataSet Connections
Let's create a master detail report using Direct Data Views. Create a new application, and set up a connection with your database using your favorite Db Framework. I'll still be using BDE, for demonstration purposes. Add two tables (or queries, as you prefer), selecting data from the orders and items tables. For each DataSet, add a corresponding TRvDataSetConnection, available in the Rave tab, in the pallete. Set the property DataSet of each DataSetConnection to the corresponding component, and give them meaningful names (I called them dscItems and dscOrders).
The name is important: it is the your application link to the report. You won't be able to have two DataSet connections, even in different reports, with the same name, or you might run into trouble if someone tries to run both reports concurrently.
After the DataSets are set up, return to the Rave Reports designer. Choose New Data Connection, and you should see the DataSet Connections you added to your form in the Delphi designer:
Select dscItems to create the first DataView, then repeat the process to create the DataView for dscOrders. You should now see the fields in the Project Treeview, under the newly created DataViews:
Don't forget to rename the DataViews to better names than DataView1 and DataView2.
Creating the Bands
After the DataViews have been created, we should set up the bands. Create a Region consisting of the whole page area, and add one band (Header), two DataBands (Master and Detail), and another Band (Footer). Set the ControllerBand property of Header, Detail and Footer to Master. Using the BandStyle property editor, set the style of the Header band to Body Header (B), the style of the Detail band to Detail (D), and the Footer band to Body Footer (b). Notice how the Band Display gives a nice preview of how the bands are going to printed:
You also need to link the DataView property of the Master band to dvOrders, and the Detail band to dvItems. To create the Master/Detail relashionship, set the MasterDataView property of the Detail band to dvOrders, and both the MasterKey and DetailKey properties to the field OrderNo.
The FontMaster Component
You proably want similar items (all the items in the Detail Band, for example), to print with the same font. Instead of setting each Font property individually, you can you a FontMaster. Add a FontMaster component to the page and set its Font propert to Times New Roman, 10. In the next step, when adding the Text and DataText components, set their FontMirror property to the FontMaster. Now, if you ever change your mind about the font that is going to be used in this report, you don't need to change it in every component, just change it in the FontMaster and it will be reflect in every other component that is linked to it. You can have as much FontMasters in one report as you need.
The FontMaster is a Non-Visual component, meaning it won't show in the Design Area. If you need to reselect it, you will have to do it through the Project TreeView.
Adding the DataTexts
Now its just a matter of adding Text and DataText components to the bands. Don't forget you can drag-and-drop (while pressing the ctrl and alt keys) from the Project Treeview to create them. I added OrderNo, SaleDate, Terms, PaymentMethod, AmountPaid and Freight to the Master band, and ItemNo, PartNo, Qty and Discount to the Detail band. Don't forget that the DisplayFormat of float fields is set in the fields themselves, in the Project Treeview, and not in the DataText components.
Adding Totals and Calculated Fields
You can make calculations and aggregated values using the CalcOP and CalcTotal components, available on the Report tab. The result of these calculations can be outputted to parameters, PI Vars, or used as intermediate result for other calculations. We are going to output them to parameters, so select your report in the Project Treeview and add two parameters, called AmountPaidTotal and FreightTotal:
Now add two CalcTotal components to your the Footer band, and set their Controller property to the Master band, their DataView property to dvOrders, the DataField to AmountPaid and Freight, and the DestParam to the params you created earlier, AmountPaidTotal and FreightTotal. It's also in the CalcTotal component where you define the DisplayFormat of the value printed.
Note that the Calc components are evaluated in the order they appear in the Project Treeview. If a CalcOp result is going to printed by a DataText component, make sure it appears in the Treeview before it. You can use the buttons Move Forward and Move Behind from the Alignment Toolbar to change their order.
You can now add in the Footer Band two DataText components to print those variables. Notice that is has summed the values, and you can define through the CalcType property of the the CalcTotal components the operation you would like to perform.
The sample data included in the DbDemos does not have a value for Freight in any of the records. Feel free to edit a few records to see them added up.
Now let's suppose we need to sum both totals, and display them in the footer band. Add a new parameter called Total to the report and a CalcOp component below the CalcTotal components, in the Footer band, and set the Src1DataField to Param.AmountPaidTotal, and Scr2DataField to Param.FreightTotal. Set the DestParam property to Total, and the DisplayFormat of the value. Add a DataText to print this value.
To add the report to your project, just do the same as was done in the previous reports: just link it in a RvProject and call the execute method. You might want to filter the DataSet based on user input, but that's the same way as you always done before.
We have seen how to create DataAware reports using Direct Data Views, how to set up master/detail relationships, and how to use Calc components. You can find a project containing the report developed here on CodeCentral. Part V will demonstrate the use of Custom Connections and some other tricks.