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 5: Using Calculated fields
 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 6: Sorting & Grouping
• 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

   Using Calculated fields
There are two ways of handling calculated fields in reports. The first way is to create calculated fields in your database when you set up your Data module. If you are using a data module for your project, definitely use it in your quick reports too. By doing this, you create one place to handle all your data and can maximize your reusability. The other method is to use the QREXPR component.

Since the creation of calculated fields in a table is beyond the scope of our tutorial, I will concentrate on using the QREXPR component. This component is fairly complex and complete in it's design but like many parts of QR it has it's quirks. The result screen in the expression builder is the memo field at the bottom of the dialog. By selecting an item in each of the sections and pressing the ADD key there you build the expression. The QREXPR has four sections:

Constant: This section is used to handle a number or string in an equation. For example, let's say you need to multiply a field by a number to get your calculated field. You would enter the number (.05) in the edit field. Leave the drop down box as numeric and press the Add button. This places the number in the bottom memo field.

Operators: This section, like the constant section, is used to add operators to the field. For example, by pressing the * button you add the * to the equation you began with the number .05. The operator section behaves much like a calculator with the ability to also handle parentheses and Boolean comparisons in your equation as well as all the other math keys.

Data Field: The data field section allows you to select a field from any table used in the report. Therefore you have a Dataset dropdown box to select your table and a listbox to select the field. One caveat: When you are building your expression, every time you add something to the expression it returns to the top of the list of tables. So be careful you have the right table and field when you press the Add button. To continue our example, you can now select a field to multiply .05 to and press the Add key to add it to the memo field. If you now press the OK button to close the dialog, you can see your expression in the Expression property of the component. By reclicking the 3 dots, you will bring the expression back into the expression builder but you can't get at it to edit it. The only way to change an equation you created is to carefully modify the string in the expression property. Otherwise you need to clear the equation and start over.

Function: This category contains a large selection of options available. Some will probably be rarely used. Others can be duplicated in code with out using the expression component. But some are essential. Here is what is available:

Function Description Usage
Average Calcs Average of a column Avg(Table1Field)
Copy Creates substring from data Copy(Table1Field,1,2)
Count* Increments a counter Can be used as a record counter
Date* Converts curr date to a string Can be used to print today's date in a column
False* Used in boolean equations Used in building statements
FormatNumeric Sets up a mask on a field FormatNumeric('###,###',Table1Field)
Frac Returns the fractional value Returns everything behind the decimal point
If Used in Equations If(Condition,True Result, False Result)
Int Returns Integer value Truncates everything behind the decimal
Lower Converts string to lower case Lower(Table1Field)
Max Calcs Maximum value of Col Max(Table1Field)
Min Calcs Minimum value of Col Min(Table1Field)
Pretty Converts 1st char to upper case Pretty(Table1Field)
Sqrt Calcs Square root of data Sqrt(Table1Field)
Str Converts number to String Str(Number)
Sum Calcs Sum of a column Sum(Table1Field)
Time* Returns current time as string Can be used to print a timestamp
True* Used in boolean equations Used in building statements
TypeOf Returns data type of expr Not sure what this can be used for
Upper Converts string to upper case Upper(Table1Field)

Note: Functions with *s are designed to be used in IF expressions. The usage example explains any other possiblities.

Working with the IF function can be the most frustrating because you can't get at the actual statement you are building. Remember, you can edit the string outside the dialog box, so if necessary, just exit out of the Expression builder and carefully make your changes.

   TIP #8
Use the Mask property to handle any formatting of your calculated expression. The Mask uses characters similar to Formatfloat function. Common masks are:

$###,###,00 for Currency , EUR##0.00 for Eurodollars.

You can also setup different masks for positive and negative numbers by placing a semicolon between them. For example: $##0.00;($##0.00);

Next page > Sorting, Grouping and Summarizing in 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.