• Aucun résultat trouvé

Database reports with Delphi and ADO − DB Course/Chapter 17

Chapter seventeen of the free Delphi Database Course for beginners. How to use QuickReport set of components to create database reports with Delphi. See how to produce database output with text, images, charts and memos − quickly and easily.

In programming (database) terminology reports are printed documents containing data from a

database. To generate reports with Delphi we generally use special reporting tools. In most cases such tools are available from third−party developers. One set of components, QuickReport from QuSoft, comes with Delphi as a set of VCL components.

In this chapter of the free database course, you'll see how to use QuickReport set of

components to create database reports with Delphi; how to produce database output with text, images, charts and memos − quickly and easily.

QuickReport is designed to let us produce database reports as well as reports based on text files and string lists. QuickReport lets us create print previews where the user can check the result of a printout without wasting paper, and export data to other file formats, such as plain ASCII, comma separated values (CSV), MS Excel XLS, Rich Text RTF and HTML. By using the components on the QReport page of the Component palette, you can visually build banded reports to present and summarize the information in your database tables and queries. You can add summaries to group headers or footers to analyze the data based on grouping criteria.

Report Types

Among many different types of database reports, three are most common:

List reports look like a table (or a DBGrid), each row containg data from a row in a recordset.

Label reports present data inside rectangular areas. This type of report is mostly used when printing envelopes.

Master−detail reports are used when report contains data from two linked recordsets. In general, several detail records follow the appropriate master record. This type of report requires parent−child relationship between two recordsets.

The simplest way to create a report is to use the QuickReport Wizard located on the Forms page (Delphi 5) of the New Items dialog. We'll pick the QuickReport List − this creates a new form with the main reporting component TQuickRep. This component acts as a container for several TQrBand components containing TQrLabels. There is also a TTable component on that form.

We are not going into details about QuickReport here, since there is a comprehensive tutorial on using the QuickReport set of components with Delphi available on this site.

Quick ADO Delphi report

Unfortunately the Wizard crates a reporting template for BDE based database application, by placing the TTable component on a form along with DB and DBTables units in the uses clause. To prepare the form for our aboutdelphi.mdb MS Access database and ADO you need to delete the TTable from the form as well as the DB and DBTables units from the forms uses clause.

We'll now create a simple list report containing data from the Applications table. First make sure the newly created form is the default for the project (Project | Options | Forms). Second add the

TADOTable component on a form, set it's Connection property to point to the aboutdelphi.mdb database (this time were not going to use ADOConnection) and set the Table property to Application.

Third, remove the TitleBand1 (TQrBand), PageFooterBand1 (TQrBand) and the ColumnHeaderBand1 (TQrBand). Also, remove the QRLabel2 from the DetailBand1. This leaves us with only ADOTable1, QuickRep1 and DetailBand1. To link QuickReport1 with ADOTable1 set it's Dataset property to point to ADOTable1.

The next step is to place several TQRDBText components on DetailBand1, one for each field we want to print, and set their Dataset property to ADOTable1. Add three QRDBText components, let them point to Author, Type and Description fields of the Applications table.

To see the report at design time set ADOTable1.Active to True, right click the QuickRep1 component and select Preview. This is something similar to what you should see:

To show this preview window at run time, we need to call the Preview method of the TQuickRep component.

procedure TQRListForm.FormCreate(Sender: TObject);

begin

QRListForm.QuickRep1.Preview;

end;

Note that when previewing the report, Delphi (QuickReport) uses the standard QuickReport preview form. If you want to change the appearance or behaviour of this form you can create your own preview form with the TQRPreview component.

To send this report to a printer use the Print method.

Charts and Images

In many cases you'll need to create reports that consist of other elements like charts or images.

Creating a report with chart is simple. Just pick the TQRChart component and use it as explained it the Charting with Databases chapter. To show a picture stored inside an Access database you should use the TQRImage (not TQRDBImage) component. Since there are some "problems" with displaying pictures inside Access you should consider the Pictures inside a database chapter.

To the next chapter

That's it. I just wanted to inform you about printing options with ADO Delphi based solutions. You should be aware that beside QuickReport components there are other reporting tools available to a Delphi developer. You can even use Automation to control MS Word and use it as a reporting tool. If you need any kind of help so far, please post to the Delphi Programming Forum where all the

questions are answered and beginners are treated as experts.