Component Based Reports

Delphi supports component-based report building by including the QReport components on the same-named tab in the VCL. These components are provided by a third-party developer, QuSoft, and are well integrated because the entire set of components is written in Delphi. The component set gives the developer complete control over the page layout, all printer settings, and output. The next set of reports that we will examine will be built using these components.

We'll begin by building a form report. A form is usually based around reporting the data of a single record and is formatted in a highly structured way so that the person receiving the report can quickly assimilate the information. The report that the next project will create is going to take the form one step further and create a master-detail report in the form of an invoice.

IS a banded report generator. This means that the report form is built up using bands, sections that contain the elements of your report and act according to their type. For example, a Detail band will automatically recognize the necessity to repeat itself when the data it is reporting contains multiple items such as with a database table. Before jumping into the fray with the Invoice report, let's re-create the simple customer listing that was coded in the last example to get a feel for the component-based process.

Start a new Delphi project. Add a Table component and set the DatabaseName property to DBDEMOS and the TableName property to CUSTOMER.DB. Activate the dataset. Add a QuickRep component to the form and set the DataSet property to Tablel. Expand the form as large as your monitor allows while not covering the Object

230 ■ Part H—The Delphi Database Tools

  1. In turn, move the QuickRep control so that you can have the maximum viewing area available. Save the project. Name the unit CustlistU.pas and the project CustList.dpr.
  2. The first band that we will add is a Detail band. This is the workhorse of the report, containing our listings. Double-click on the QRBand button to place the control on the form.
  3. Next, we need to add the data fields to the band. Double-click on the QRDBText button three times to place the field components on the band. Select all three and set the DataSet property to Tablel. Deselect the group and, as appropriate to the layout of the report, set the DataField property of the fields to CustNo, Company, and Country,
  4. Right-click on an open area of the QuickRep control to invoke the context menu and select Preview. The preview function lets you see the WYSIWYG layout of your report. At this point you should have a complete customer list. If not, examine the settings of all of the controls on the report to ensure that all of the properties are set as needed.
  5. The spacing on the report seems a little wide for our purposes. The size of each line is controlled by the height of the Detail band. Move the field components to the top of the band and then select the band itself. Grab the handle at the bottom center of the frame and pull it upward to tighten up the spacing. Use the preview function to examine your changes and modify as necessary to suit your tastes.
  6. Select the QuickRep form again. Click on the Bands property to expand it and set the HasTitle property to True. Notice that when you do, a new band is added to the report with its type set to Title. This is an alternate method to add new bands to the report. A band of type Title will print once on the first page of the report. This is different from a page header that will print on all pages.
  7. Double-click on the QRLabel button to add this control to the Title band. Set the caption for this component to DIVERS SUPPLY CO. CUSTOMER LIST Set the Alignment property to taCenter and the AlignToBand property to True. These settings will have the effect of centering the report's title in the band.
  8. The last thing that we need to add is the time and date and the page number. Because we want to have this information appear on every page of the report, we are going to use a PageHeader band. Add a new band to the report, using either method, and set the BandType property to rbPageHeader. Did you notice that as bands are added to the QuickRep form they position themselves correctly?
  9. Add a QRSysData component to the form. These controls obtain system information to add to your report. Align the control to the upper

Chapter 7—Reporting and Printing

Chapter 7—Reporting and Printing

left-hand corner of the PageHeader band. Set the Data property to qrsDate so the current date will be automatically added to the report each time it is run. Now add a QRLabel component to the band and set its caption to PAGE:. Add another QRSysData control just to the right of the Page label and set its Data property to qrsPageNumber. Preview the report again. While you are previewing the layout, move to the second page to confirm that the title does not appear.

Only one thing is missing-the column headings, and QuickReport has us covered there as well. Add a new band to the report and set its type to rbColumnHeader. To this band, add three QRLabel controls to the new band and caption each in order, CustNo, Company, and Country, To set these labels off from the detail, they should appear in a bold typeface. Select all three controls and expand the Font property and then the Style property. Set the fsBold property to True.

Rather than underline each as we did on the coded report, we are going to add a single line to separate the labels from the data. Each of the bands is contained in a frame and we can use this frame to draw lines or boxes with. Select the ColumnHeader band and expand its Frame property. Set the value of the DrawBottom property to True. Preview the report and print it if you like. Your project should look something similar to Figure 7.3 with the final report appearing in Figure 7.4.

Figure 7.4 The final report

Figure 7.4 The final report

All of this work has been done in the design mode of our project. To activate the report in the project is a simple matter using the met hoi of the QuickRep component. Since the size of the QuickRep control has no bearing on the way that it prints, size it down a little so that some of the underlying form is visible and add two button controls the form. Caption one Preview and the other Print.

Double-click on the Preview button and add the following line to the j event handler:

QuickRepl.Preview;

Do the same to the Print button and add this line:

QuickRep 1 .Print;

This completes the customer list project. Save your work.

One additional note is in order about the bands. These components are container objects, meaning that they own the other components placed within them. Once you place a field or other object, you will not be able to simply drag it to a different band. Also, remember that when a container object is deleted, it takes all controls and settings with it. .

Was this article helpful?

0 0

Post a comment