1. Home
  2. Computing & Technology
  3. Delphi Programming
RTL reference|Glossary|Tips/Tricks|FREE App/VCL|Best'O'Net|Books|Link To
 
Quick Reports Tutorial
Page 6: Sorting, Grouping and Summarizing in Reports
 More of this Feature
• Page 1: Reports Overview
• Page 2: What's in a report
• Page 3: History of QR
• Page 4: Band reports
• Page 5: Calculated fields
• Page 7: Label Reports
• Page 8: QR components
• Page 9: Without the QR
 Join the Discussion
"Post your questions, concerns, views and comments to this article..."
Discuss!
 Related Resources
• Reporting Tools
• Printing with TPrinter
• Controling MS Office

   Sorting, Grouping and Summarizing in Reports
Once you make it past the most basic of your reports, you will need to know how to sort, group and summarize data in your report. Quick report does not handle sorting internally (see the next section on how to handle this). QR uses the Group header and footer components to handle grouping and the QREXPR to handle summarizing. Stay tuned as we explore this important area.

Sorting on Data
Sorting must be handled outside of Quick Reports. This is not as big a deal as it may sound at first. Simply place a query component on your form and create your query with any sorting you need. Using querys is the simplest way to handle master / detail reports also. A good way to setup your SQL is to create your query using the Database desktop's query by example generator. Then when you get it the way you want it, press the show SQL button and copy the code into the Query components SQL strings. Test your query by making it active. If you can do this you should be all set.

Grouping Data
Grouping data requires a QRGroup component. This is a Header component. Optionally you can have a QRFooter component. This is done by dropping a QRBand component and changing it to a QRFooter. Lets talk about the QRGroup component first.

The QRGROUP component
There are a lot of properties in a QRGroup component but most you will only rarely use. The most used ones (and the most important ones are:

Expression - This property determines the conditions for separating one group of records from another. Usually you will simply need to enter the field in the table you are grouping on. Occasionally you will need to use more complex conditions. Since the expression dialog is the same as the QREXPR, I will not repeat how to use it here. Remember, you need a separate QRGroup header for each break you want in your report. Also each QRGroup needs to be seen from the first break inward to the last break. This is fairly intuitive, but occasionally you will make a mistake and put in the wrong field or sometimes you will need to change a grouping field in a component. Whatever the case DON'T change the position of the QRGroup component by moving it and don't leave it where it is and hope that QR will figure out that it groups these fields first and those fields second etc. What you will have to do is change the expressions of each component so that the correct order is maintained. Otherwise, the only alternative is to delete each QRGroup component and read them in the order you want.

Font - This property is useful if most of the components going into this band are going to look similar. Be sure to change this right after you place the component so that all subsequent components use this property when they are placed.

FooterBand - This is where you link the header band to the correct footer band and encapsulate your group inside. I recommend placing all your header bands first and then go back and place the footer bands by dropping a QRBand component and immediately changing the type from rbTitle to rbGroupFooter. Next name this band a unique name so that you can understand easier which footer goes with which header and why. Once you have done this, you can go back to the QRGroup header and select the right footer band to use. Also, notice that each footer does not automatically sort itself out once it is linked to a group band, so go from the inside out in your grouping so that the footers work correctly.

ForceNewColumn and ForceNewPage - Usually you would change this to true in the Footer band if you wanted a new page. You should not do this on a Group header.

The QRFooter Band
The QRFooter band which is called the rbGroupFooter in the band type drop down is needed with a header band only if you want the entire group to appear on one page. If you have no other use for the footer band, then make it as small as possible to save paper. The other use is when you are printing summary totals for each group.

Using TQRExpressions in summarizing data
Speaking of summary totals, the most common use for TQREXPR components is to sum data. The QRExpr component has several aggregate functions and they usually work the same way so we will concentrate on a few of the key properties of QRExpr that affect summing data.

Master - The Master property is used to setup the input to the expression. If you are summing a field in a table then the master property must be set to this table. Setting a master to the wrong table or leaving it blank causes problems .

ResetAfterPrint - This property has caused so much difficulty because the default is false. If you want each group totaled separately you need to change the setting to true. Otherwise the total will continue to grow cumulatively.

Alignment - Change this to TaRightJustify for numeric output. It is also a good idea to change the AutoSize property to false and manually set the width of this field. You will need to ignore the text inside the field because it has no connection to what the actual output will look like.

Mask - Use this property to set the formatting of your expression. See the earlier section on how to do this.

   TIP #9
Sometimes you wish to put a line on top of the summary numbers. You can use a QRShape component set as a line or you can use the Frame property to put a line over the top. Set the DrawTop property to true and it will put a line over each of the numbers that are printed. You can change the style and thickness of the line there too.

Next page > Label reports > Page 1, 2, 3, 4, 5, 6, 7, 8, 9

All graphics (if any) in this feature created by Zarko Gajic.

 More Delphi
· Learn another routine every day - RTL Quick Reference.
· Download free source code applications and components.
· Talk about Delphi Programming, real time.
· Link to the Delphi Programming site from your Web pages.
· Tutorials, articles, tech. tips by date: 2001|2000|1999|1998 or by TOPIC.
· NEXT ARTICLE: Your application will be shut down!.
Access violations, invalid page faults, general protection faults; the name changes, but the nature of the problem is always the same. Find the solution to your Delphi AVs.
 Stay informed with all new and interesting things about Delphi (for free).
Subscribe to the Newsletter
Name
Email

 Got some code to share? Got a question? Need some help?
Explore Delphi Programming
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming

©2009 About.com, a part of The New York Times Company.

All rights reserved.