The Variable Component and Calculation Popup Window
Top  Previous  Next

A variable component is useful because it can display and store data that is based upon code and calculations. When you add one to your report you need to set the return value type (integer, string etc) so that i) the report knows how to display / format it, and ii) your code knows what value types to assign to it; iii) other procedures that refer to it know what value type it returns. You specify the return value type in a drop down control on the Toolbar.

Once you've assigned a return value type you need to assign code to the variable component. You can do this on the Calc Tab, but a shortcut option is to invoke a special code popup window by right-clicking on the component and selecting "Calculations". This will provide you with a ready made procedure for the component's OnCalc event. The OnCalc event fires when the calculation for the component needs to be performed. By default this event occurs once for each record traversal completed by the report engine. You can control exactly when the component is calculated and when it is reset by setting the CalcOn and Reset properties in the Timing form, which you can invoke by right clicking on the component and selecting "Timing.."

Now lets look at variables in a practical example: it might be desirable to show the amount of discount (in currency, not rate) that you are giving a customer. You may or may not want to display this on a line by line basis, but certainly it needs to be calculated for each line and a running total kept so that a total discount can be displayed in the report footer. The are a few ways that this can be done; first lets assume that you want to show the discount on a line by line basis as well as a total:

1.In your Quote report drop a variable component reportvar1 into the Details Band between the List Price and Charged Price fields. Set the return value of the variable to be Currency. We note that the variable is somewhat anonymously called "variable1", which may not be too user friendly when we come back to work on the report again one day; so lets rename it.  
2.From the View | Toolbars menu select Report Tree. Find the variable1 component in the TreeView displayed, right click on it an select Rename. Lets call it "LineDiscount". Close the Report tree if it is no longer needed.  
3.Right click on the variable and select Calculations. Use the ToolWindow to drag the 3 data fields that you need to calculate the line discount so that you end up with procedure code like this:  

report37  

Note: in the Calculations Window, you don't get to see the procedure header. If you were to close this and then find your LineDiscount component in the Calc Tab, then against its OnCalc event you would be:

report38  

Preview the report to see the results.

4.Now add another variable component reportvar1 to the report but this time to the Summary Band. Rename it to be DiscountTotal and set its return value type to be Currency. Set its Calculations code to be:  

Value:= Value + (List['Eqlist Detail-q Request']*List['Eqlist Detail-list Price']) - List['Eqlist Detail-charged Price'];  

Note that all we are doing here is keeping a running total going be adding Value to itself plus any discount on the line.

Now that you have the Total Discount on the report you will probably now wish to replicate the above strategy so that you can also display the List Price Total in the Summary Band as well.