p> Users often generate reports when using database applications. Using the QReport component of Delphi 4 can help us generate reports quickly and easily. Here, we use a device governance report as an example to illustrate how to use the QReport component and the Query component to design to generate reports from multiple data tables.
1. Database used
Here we use three FoXPRo data tables, DLBMK (equipment category code), SBXHK (equipment model and configuration), and BMSBK (equipment department), which are stored in the D:/SB directory. The library structure is as follows:
(I) DLBMK field name field type explanation 1 DLBH N3 device category number 2 DLMC C20 device category name (II) SBXHK field name field type explanation 1 XHBM N3 device model code 2 DLBH N3 DLBH field in the same DLBMK 3 SBXH C30 Device Model 4 SBPZ C30 Device Configuration 5 SBSL N3 Number of Devices (III) BMSBK Field Name Field Type Explanation 1 BMMC C20 Department Name 2 XHBM N3 XHBM Fields in SBXHK 3 SL N3 Quantity Use these three data tables to Generate a device model that is only available in the computer department but not in other departments.
2? Tapes and machinery, Moukaofen?/b>
There are two forms in the program: mainForm mainForm and report form repForm. There are two TButton parts in the mainForm mainForm, and the settings are as follows:
Part properties and attribute values
PreviewBTn:TButton Caption:Preview
PrintBtn: TButton Caption: The components and properties in the print report form repForm are set as follows:
Part properties and attribute values
Query1: TQuery DatabaseName:d:/sb
Active: True
Qrep1: TQuickrep Dataset:query1
TitleBand1: TQRBand BandType:rbTitle
HeadBand1: TQRBand BandType:rbColumnHeader
DrawLeft : True
DrawRight : True
DrawTop : True
DrawBottom : True
DetailBand1: TQRBand BandType:rbDetail
DrawLeft : True
DrawRight : True
DrawTop : True
DrawBottom : True
ChildBand1: TQRChildBand ParentBand:DetailBand1
DrawLeft : True
DrawRight : True
DrawTop : True
DrawBottom : True
TitleLabel: TQRLabel Caption: Device Statistics Table
DlmcLabel: TQRLabel Caption:Category
SbxhLabel: TQRLabel Caption: Model
SbpzLabel: TQRLabel Caption:Configuration
SbslLabel: TQRLabel Caption: Quantity
DlmcDBText: TQRDBText Dataset:Query1
Datafield: dlmc
SbxhDBText: TQRDBText Dataset:Query1
Datafield: sbxh
SbpzDBText: TQRDBText Dataset:Query1
Datafield: sbpz
SbslDBText: TQRDBtext Dataset:Query1
Datafield: sbsl
Shape1~9: TQRShape Shape:qrsVertline
Top:0
Width:1
The SQL property of Query1 is set to:
select a.dlbh,a.dlmc,b.sbxh,b.sbpz,b.sbsl
from dlbmk a,sbxhk b
where a.dlbh=b.dlbh and b.xhbm not in
(select xhbm from bmsbk where trim(bmmc)$#@60;$#@62;'Computer Department')
order by a.dlbh Set the DrawLeft, DrawRight, DrawTop, and DrawBottom attribute values of several TQRband components to True, in order to print the table border and horizontal lines. The TQRShape component is used to print out the vertical lines of the table. DlmcDBText is placed on DetailBand1, several other TQRDBText components are placed on ChildBand1, Shape1~3 is placed on HeadBand1, Shape4~6 is placed on DetailBand1, Shape7~9 is placed on ChildBand1.
3. Add code to the program
1. Two button events in mainForm Procedure TmainForm.PreviewBtnClick(Sender: TObject)
Begin
repForm.Qrep1.Preview;
end;
procedure TmainFormPrintBtnClick(Sender: TObject)
Begin
repForm.Qrep1.Print;
end;
2. BeforePrint events of HeadBand1, DetailBand1 and ChildBand1 are processedure TrepForm.HeadBand1BeforePrint(Sender: TQRCustomBand; Var PrintBand: Boolean)
Begin
Shape1.Height:=HeadBand1.Height;
Shape2.Height:=HeadBand1.Height;
Shape3.Height:=HeadBand1.Height;
End;
procedure TrepForm.DetailBand1BeforePrint(Sender: TQRCustomBand; Var PrintBand: Boolean)
Begin
PrintBand:=bh$#@60; $#@62;Query1['dlbh'];
if PrintBand then
Begin
bh:=Query1['dlbh'];
Shape4.Height:=DetailBand1.Height;
Shape5.Height:=DetailBand1.Height;
Shape6.Height:=DetailBand1.Height;
end
end;
procedure TrepForm.ChildBand1BeforePrint(Sender: TQRCustomBand; Var PrintBand: Boolean)
Begin
Shape7.Height:=ChildBand1.Height;
Shape8.Height:=ChildBand1.Height;
Shape9.Height:=ChildBand1.Height;
End;
bh should be defined in the variable definition section: Var bh : shortint=0;
The height of several TQRShape parts is consistent with the TQRBand parts, making the vertical lines print neatly. If the height of the TQRBand component is adjusted during the design stage, there will be no vertical line breakage or excessive length.
Controlled with PrintBand in the BeforePrint event of DetailBand1, the major category name of each device can be printed only once, rather than the major category name corresponding to each model. Because the values of the dlbh field are greater than 0, the initial value of bh is set to 0 so that it is different from the values of the dlbh field of any record to ensure that the first major class name is printed. This generates a data report that extracts data from multiple data tables and has table lines.
Note: The project file in this example is sbgl.dpr, and the original program files are main.pas and sbrep.pas. If you want to test, please place the three files dlbmk.dbf, sbxhk.dbf, and bmsbk.dbf in the d:/sb directory. .