Computed fields are very useful; they let the client PC perform client-side calculations that, for whatever reason, are not being performed on the server. There may be times when you want to perform certain DataWindow functions that work with computed columns but not with computed fields. This document will explain how to save data from a computed field using the DataWindow saveAs( ) function, using both a computed field and a computed column.
Suppose you had a database table defined as follows:
|
Field
|
|||
| V a l u e |
Name |
Salary |
Bonus |
| Joe |
30000 |
500 |
|
| Rick |
45000 |
1500 |
|
| Sue |
48000 |
1000 |
|
The DataWindow SQL select statement
for the above Employee table might be:
SELECT Name, Salary,
Bonus FROM Employee ;
You may want to compute the total salary for each employee on the client by using a COMPUTED FIELD, named say,"cf_total_salary" that adds the Salary and Bonus columns and places it on the Detail Band with the rest of the columns.
i.e.: cf_total_salary = salary + bonus
The DataWindow with the new computed field would look something like this:
|
Field
|
||||
| V a l u e |
Name |
Salary |
Bonus |
Total |
| Joe |
30000 |
500 |
30500 |
|
| Rick |
45000 |
1500 |
46500 |
|
| Sue |
48000 |
1000 |
49000 |
|
If you wanted to save the contents of this DataWindow, you could issue a this.SaveAs() in one of the DataWindow events. However, this would NOT save the contents of the computed field to the file. Only columns in your DataWindow select statement are saved when you issue a SaveAs() command.
To save the contents of the computed field, you could define a dummy computed COLUMN as part of the DataWindow's select statement.
To add a numeric computed column to your DataWindow, simply add a number to the end of your SQL select statement. In the example below, the third column is a computed field.
SELECT Name, Salary, Bonus, 100 FROM Employee ;
The above statement will add a computed column to the DataWindow that will serve as a placeholder for storing our computed field into. You will need to delete the column from the detail band of the DataWindow so that it will not display on the DataWindow.
The idea here is that for each row coming from the server, we will save the contents of the computed field into a local variable and then use setitem() to populate the computed column. The SaveAs() will then save the computed COLUMN.
Here is the DataWindow script code you'll need to move the contents of the computed FIELD to the computed COLUMN:
//
// RetrieveRow
event code
//
long ll_computed_field_value
// Store the computed FIELD in
the computed COLUMN compute_0001
ll_computed_field_value
= this.getitemnumber(row,"cf_total_salary")
this.SetItem(row,"compute_0001",ll_computed_field_value)
and save it to a file:
//
// RetrieveEnd
event code
//
// Save the DataWindow
to a file
this.SaveAs()

Back to Top