Defining a computed field in the DataWindow painter Design view

StepsTo define a computed field in the DataWindow painter Design view:

  1. Select Insert>Control>Computed Field from the menu bar.

  2. Click where you want to place the computed field.

    If the calculation is to be based on column data that changes for each row, make sure you place the computed field in the detail band.

    The Modify Expression dialog box displays, listing:

    • DataWindow expression functions you can use in the computed field

    • The columns in the DataWindow object

    • Operators and parentheses

  3. Enter the expression that defines the computed field as described in “Entering the expression”.

  4. (Optional) Click Verify to test the expression.

    PowerBuilder analyzes the expression.

  5. Click OK.

Entering the expression

You can enter any valid DataWindow expression when defining a computed field. You can paste operators, columns, and DataWindow expression functions into the expression from information in the Modify Expression dialog box. Use the + operator to concatenate strings.

You can use any built-in or user-defined global function in an expression. You cannot use object-level functions.

NoteDataWindow expressions You are entering a DataWindow expression, not a SQL expression processed by the DBMS, so the expression follows the rules for DataWindow expressions. For complete information about DataWindow expressions, see the DataWindow Reference.

Referring to next and previous rows

You can refer to other rows in a computed field. This is particularly useful in N-Up DataWindow objects when you want to refer to another row in the detail band. Use this syntax:

ColumnName[x]

where x is an integer. 0 refers to the current row (or first row in the detail band), 1 refers to the next row, –1 refers to the previous row, and so on.

Examples

Table 20-1 shows some examples of computed fields.

Table 20-1: Computed field examples

To display

Enter this expression

In this band

Current date at top of each page

Today()

Header

Current time at top of each page

Now()

Header

Current page at bottom of each page

Page()

Footer

Total page count at bottom of each page

PageCount()

Footer

Concatenation of Fname and Lname columns for each row

Fname + " " + Lname

Detail

Monthly salary if Salary column contains annual salary

Salary / 12

Detail

Four asterisks if the value of the Salary column is greater than $50,000

IF(Salary> 50000, "****", "")

Detail

Average salary of all retrieved rows

Avg(Salary)

Summary

Count of retrieved rows, assuming each row contains a value for EmpID

Count(EmpID)

Summary

For complete information about the functions you can use in computed fields in the DataWindow painter, see the DataWindow Reference.

Menu options and buttons for common functions

PowerBuilder provides a quick way to create computed fields that summarize values in the detail band, display the current date, or show the current page number.

StepsTo summarize values:

  1. Select one or more columns in the DataWindow object’s detail band.

  2. Select one of the options at the bottom of the cascading menu: Average, Count, or Sum.

    The same options are available at the bottom of the Controls drop-down toolbar on the PainterBar.

    PowerBuilder places a computed field in the summary band or in the group trailer band if the DataWindow object is grouped. The band is resized automatically to hold the computed field. If there is already a computed field that matches the one being generated, it is skipped.

StepsTo insert a computed field for the current date or page number:

  1. Select Insert>Control from the menu bar.

  2. Select Today() or Page n of n from the options at the bottom of the cascading menu.

    The same options are available at the bottom of the Controls drop-down toolbar on the PainterBar.

  3. Click anywhere in the DataWindow object.

    If you selected Today, PowerBuilder inserts a computed field containing this expression: Today(). For Page n of n, the computed field contains this expression: 'Page ' + page() + ' of ' + pageCount().

Adding custom buttons that place computed fields

You can add buttons to the PainterBar in the DataWindow painter that place computed fields using any of the aggregate functions, such as Max, Min, and Median.

StepsTo customize the PainterBar with custom buttons for placing computed fields:

  1. Place the mouse pointer over the PainterBar and select Customize from the pop-up menu.

    The Customize dialog box displays.

  2. Click Custom in the Select palette group to display the set of custom buttons.

  3. Drag a custom button into the Current toolbar group and release it.

    The Toolbar Item Command dialog box displays.

  4. Click the Function button.

    The Function For Toolbar dialog box displays.

  5. Select a function and click OK.

    You return to the Toolbar Item Command dialog box.

  6. Specify text and microhelp that displays for the button, and click OK.

    PowerBuilder places the new button in the PainterBar. You can click it to add a computed field to your DataWindow object the same way you use the built-in Sum button.