Skip to Content
Shared variables are a great way to pass data between the subreport and the main report. But you have to be careful because using them requires following certain rules. Let’s review what these rules are.

The first two rules actually work together. The first rule is that variables are calculated in the order that they are printed on the report. The typical printing order is to start by printing the page header, followed by any group headers, followed by the detail records, etc. The formulas included in each section are not calculated until that section prints. So, formulas in the group header are printed before formulas in the details section.

The second rule is that within a particular section, the formulas in the main report are calculated before printing the subreport. Even if a formula is displayed after the subreport, if they are in the same section, then the main report formula is calculated prior to the subreport running. If the formula in the main report uses a shared variable from the subreport, then the shared variable will not have the correct value because it hasn’t been calculated in the subreport yet. This is the source of many problems because when looking at the report you see that the subreport is shown first and you assume that its formula is passing the correct value to the formula in the main report. You can easily forget that the subreport is called last.

The best way to design reports that use subreports with shared variables is to have a separate section for each subreport. Don’t put any fields from the main report in this section. This forces each section to finish printing and calculating its formulas prior to moving to the next section. This prevents any potential confusion that arises from having a subreport’s formulas calculating after the main report’s formulas.

The third rule of subreport formulas is that they don’t get reset by themselves. Another common assumption is that each time a subreport is printed that all its variables are reset back to zero. This isn’t the case for shared variables because their values stay alive for the entire life of the main report. If the subreport doesn’t have any records to print, then the shared variable keeps the same value from the last time the subreport printed. The main report will start printing incorrect totals because its formula is basing its calculation on a value from the previous subreport. To correct this, you have to reset the variable to zero at the top of the report. This is usually done in the report header, but sometimes in the group header. And don’t forget to use the WhilePrintingRecords function to force it to call the formula each time.

If you keep these three rules in mind when using shared variables, your reports should be trouble free.

To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply