Progress
Programming
Handbook


Using Calculated Fields

A browse widget can show a calculated result of browse fields as a separate column. This is accomplished by adding the expression to the DEFINE BROWSE statement. All valid browse format phrase options are legal extensions to the expression (for example, a LABEL option). When the browse is opened, the calculated field is displayed for each row.

For updatable browse widgets, however, you need to refresh the calculation if one of the fields in the expression changes. The browse will display the appropriate calculations only when the query refreshes the data. It is more appropriate to programmatically refresh the calculated data when the user finishes editing the affected row. To accomplish this, use a base field as a placeholder for the expression. You then can reference the calculation and refresh the result as needed.

The code example below sets up a calculated field using a base field in the browse widget. The LEAVE trigger on the browse column forces a refresh of the calculated data as soon as the user leaves the browse cell:

p-br04.p
DEFINE VARIABLE credit-left AS DECIMAL LABEL "Credit Left". 
DEFINE QUERY q1 FOR customer SCROLLING.DEFINE BROWSE b1 QUERY q1 DISPLAY 
cust-num name credit-limit balance  
    (credit-limit - balance) @ credit-left 
    ENABLE credit-limit WITH 10 DOWN SEPARATORS  
    TITLE "Update Credit Limits". 
DEFINE FRAME f1 
    b1 
        WITH SIDE-LABELS ROW 2 CENTERED NO-BOX.  
ON LEAVE OF credit-limit IN BROWSE b1 DO: 
    DISPLAY (INTEGER(customer.credit-limit:SCREEN-VALUE) - balance)  
            @ credit-left WITH BROWSE b1. 
END.  
OPEN QUERY q1 FOR EACH customer NO-LOCK. 
ENABLE ALL WITH FRAME f1. 
WAIT-FOR WINDOW-CLOSE OF CURRENT-WINDOW. 

NOTE: With calculated fields in the DISPLAY phrase, you cannot use the ENABLE ALL option, unless you use the EXCEPT option to exclude the calculated field, since calculated fields cannot be edited.

When you run this example, the following browse appears:


Copyright © 2004 Progress Software Corporation
www.progress.com
Voice: (781) 280-4000
Fax: (781) 280-4095