How to Create Calculated Items in Oracle Forms

Formula items and summary items in Oracle Forms 14.1.2: how to compute amounts, totals, and balances, and what summaries need to work.

Some values on a form are never typed: a line's amount, an invoice total, the balance still owed. In Oracle Forms, these are calculated items, and Forms keeps them up to date on its own as the user types.

This guide shows the two kinds of calculated items in Oracle Forms 14.1.2, formula items and summary items, using an invoice form whose line amounts, totals, and balance follow every keystroke. It also covers the two requirements that trip up most summary items.

Sample Form for This Guide

The examples and screenshots use the sample form CH09_INVOICE from the Oracle Forms code repository on GitHub. Download it, open it in Forms Builder, and connect as CAREWELL to follow along.

FormFileWhat it shows
CH09_INVOICEforms/ch09/ch09_invoice.fmbFormula and summary items for line amounts and totals

The forms run against the CareWell Clinic sample schema, which you install first.

Formula vs. Summary Items

Formula itemSummary item
Calculation ModeFormulaSummary
ComputesOne PL/SQL expressionA statistic of one item over all records of a block
RecalculatedWhen an item it uses changes, and when a record is queriedWhen the summarized records change
ExampleA line's amount, the balanceThe total of the lines, the total paid

A calculated item computes its value, so the user never types it. It is usually a display item, and it is not a database item. You set it up in the Calculation group of the Property Palette.

Oracle Forms invoice form with formula and summary items updating as the user types
CH09_INVOICE while a line is being added: its amount and the totals are computed as the user types.

Create a Formula Item

A formula is a single PL/SQL expression, with no semicolon and no statements. It can use items of the form, functions, and program units.

The formula of the AMOUNT item, with Calculation Mode set to Formula:

:invoice_lines.quantity * :invoice_lines.unit_price

Forms recalculates the item whenever an item the formula uses changes, and when a record is queried. In the screenshot, the third line's amount appeared as soon as the user typed its price.

Keep formulas free of side effects. A formula must not change other items or the database, and it cannot call a function that does.

Create a Summary Item

A summary computes a statistic of one item over all the records of a block. Set three properties:

  • Summary Function: Sum, Average, Count, Maximum, Minimum, Standard Deviation, or Variance.
  • Summarized Block: the block whose records it summarizes.
  • Summarized Item: the item it summarizes.

The item LINES_TOTAL is the sum of INVOICE_LINES.AMOUNT, so it is a summary of a formula item.

Property Palette of the Oracle Forms summary item LINES_TOTAL with Summary Function Sum
A summary item: the sum of the lines' amounts.

Two Requirements for Summary Items

A summary adds up the records the block holds, so Forms must hold all of them. That leads to two requirements:

  1. The summarized block must have Query All Records set to Yes, so a query fetches every row instead of the first screenful. Alternatively, set Precompute Summaries to Yes, and Forms computes the summary with a separate aggregate query.
  2. The summary item must be in the summarized block itself, or in a control block whose Single Record property is Yes.

CH09_INVOICE puts its totals in a control block called TOTALS: LINES_TOTAL, PAID (the sum of PAYMENTS.AMOUNT), and BALANCE.

Combine Them: a Formula over Summaries

BALANCE is a formula item that uses the two summaries, with NVL so an invoice with no payments still shows a balance.

The formula of BALANCE:

nvl(:totals.lines_total, 0) - nvl(:totals.paid, 0)

For invoice 3001, the form shows the same totals as the database view INVOICE_TOTALS: 92.00 billed and 46.00 paid. While the user adds a line, the total and the balance follow each keystroke.

When Not to Use a Summary Item

Summary items are only as good as the records the block holds. For large blocks, fetching every record just to add them up is expensive, and an aggregate query in a trigger is cheaper.

SituationBetter choice
A few dozen detail records, such as invoice linesA summary item, with Query All Records
Thousands of recordsPrecompute Summaries, or an aggregate query in a trigger
A value that depends on other items of the same recordA formula item

The item properties that calculated items share with other items, such as format masks and justification, are covered in how to use text items in Oracle Forms. Query All Records and the other block properties are explained in how to create data blocks in Oracle Forms, and the invoice form's master-detail structure in how to create a master-detail form.

Conclusion

Calculated items in Oracle Forms come in two kinds. Formula items compute a single PL/SQL expression and recalculate whenever the items they use change, while summary items compute Sum, Average, Count, and other statistics over a block. A summary needs Query All Records or Precompute Summaries on the summarized block, and must live in that block or in a single-record control block. Combine them, as the invoice's BALANCE does, and your totals stay correct with every keystroke.

Vinish Kapoor
Vinish Kapoor

An Oracle ACE and software veteran with 25+ years of experience, passionate about AI and IT innovation.

guest

0 Comments
Oldest
Newest Most Voted
00