Defining Report Variables
To manipulate data- and display-calculated results in a report, you can use report variables. Using report variables, you can calculate values and then use those values to calculate subsequent values.
To define a report variable
Open or create a report.
From the Report menu, choose Variables.
In the Report Variables dialog box, select the Variables box and type a name for the variable.
In the Value to store box, type a field name or any other expression.
If appropriate, select a calculation option.
If appropriate, in the Initial value box, type an expression that sets the initial value.
Choose OK.
You can use the variable in any expression you enter in the report.
To count all the Canadian entries in the Company table, use this expression and select Count as the calculation option.
IIF(country="Canada",1,0)
The following example shows three variables for a simple time sheet:
To store this value | Create this variable | Using this expression |
---|---|---|
Time employee arrived |
|
|
Time employee left |
|
|
Total time employee was present |
|
|
You can use the tDayTotal
variable in a variety of other calculations, such as the number of hours worked in a week, a month, or a year; the average number of hours worked each day; and so on.
For examples of report variables, see the reports Percent.frx and Invoice.frx in the Visual FoxPro \Samples\Solution\Reports directory.
Reordering Report Variables
Report variables are evaluated in the order that they appear in the list and can affect the value of expressions that use them. For example, if variable 1 is used to define the value of variable 2, variable 1 must appear before variable 2. In the previous time sheet example, tArrive
and tLeave
must precede tDayTotal
.
To change the order of report variables
- From the Report menu, choose Variables.
- In the Variable box, drag the button to the left of the variable to rearrange the order.
- Choose OK.
Setting a Variable's Initial Value
If you use a variable in calculations, be sure that you initialize the variable with a non-zero value to avoid a division-by-zero error. If you don't specify a value, Visual FoxPro assigns a default value of 0.
To set a variable's initial value
- From the Report menu, choose Variables.
- In the Variable box, select the variable you want to set.
- In the Initial value box, enter the value.
- Choose OK.
If you reorder the groups in your report, your report variables might not be resetting on the correct field. For example, if your report has two groups, the first grouping by country and the second grouping by date, and you switch the order of the groups, the variables still reset according to the original positions of the groups.
You can change the value of a calculation by specifying when the variable is reset. By default, Visual FoxPro resets the report variables at the end of the report.
To reset a variable at the end of a report, a page, or a column
- From the Report menu, choose Variables.
- In the Reset at box, choose an option.
- Choose OK.
To reset a variable on entry or exit of any band
- In the Report Designer, open the report.
- Double-click the bar for the report band.
- In the Run expression area of the band's dialog box, choose the dialog button at the end of the On entry or On exit box.
- Enter an expression to reset the variable each time the band is entered or exited.
See Also
Using Expressions and Functions in Field Controls | Formatting Field Controls | Adding Reports and Labels | SELECT - SQL | Selecting, Moving, and Resizing Report Controls