SSRS Pull Variables Or Values From Sub Report Into

2019-02-21 12:19发布

I have a main report with several sub reports, each of these with slightly different queries and different ways to show the data.

So, in my situation, I have a textbox that needs to compile data from a few different reports with varying criteria. E.G.

MainReportTextbox =(Sum(columnA, "Main Dataset"))-(SubReportTextBox))

OR

MainReportTextbox =(Sum(columnA, "Main Dataset"))-(subReportVariable))

I saw a few suggested solutions, such as this. Which uses the =[Reports]!MainReport!SubReport!Textbox scheme. The problem is that [Reports] is not a recognized identifier.

I did consider to scrap sub reports and just have everything run on the same main report, but we lose the functionality of being able to use the reports individually, without maintaining the same thing in two places.

So I guess my question is, can you pull variables or element(particularly textboxes in a table) values from sub reports?

If the answer is simply no, please show me some information about why it is no or how it is no from MSDN or a valid source and give some valid counter suggestions.

2条回答
Luminary・发光体
2楼-- · 2019-02-21 13:05

Create variable in main report and update it in sub report so you can get value back to main report

ex: Create formula in main report with name {@Total} place flowing in it

WhilePrintingRecords;
shared Numbervar myTotal := 0;

NOTE : placing ; will not print value and without ; will print value in above example value will not be printed if you want to print value of formula just remove ; from second line ex

WhilePrintingRecords;
shared Numbervar myTotal := 0

now place {@Total} in report header of your main report now create second formula in sub report where you want to add subtotal to main report formula with name {@addTotal} place following lines in it

WhilePrintingRecords;
shared Numbervar myTotal;
myTotal := myTotal + 200; //or any formula or field

add this formula to place in sub report where you want to add value to total

now create formula in main report to show grand total with name {@showTotal} and place following lines in it

WhilePrintingRecords;
shared Numbervar myTotal;
myTotal

place {@showTotal} in your main report where you want to show this value in report but remember one thing you should place this formula after sub-report.

NOTE : to assign value to variable use := operator

查看更多
该账号已被封号
3楼-- · 2019-02-21 13:07

The links in the question and comments sometimes refer to non-SSRS reports: the syntax [subreport].[Report]![MyFieldName] or [Reports]![YourReportName]![YourSubReportName]![TheValueFromTheSubReportYouWantToReference] are not used in SSRS. It is, however, used in designing MS Access reports, as ojeffrey points out in the discussion you link to.

There is no common method to access data in a subreport. The SSRS model is that parent report data is processed, subreport data is processed, the subreports are rendered, results go back to the parent, then parent is rendered, including the subreport as appropriate. The only data passed between the two is parameters are passed into the the subreport, and rendered output is passed back to the parent. You'll see the that data passed in from the parent must be as report parameters here: http://technet.microsoft.com/en-us/library/ms160348(v=sql.100).aspx

All parameters that are required by the subreport must be included in the Parameters list. If a required parameter is missing, the subreport is not displayed correctly in the main report.

For citing authoritative sources: This discussion sums it up:

No, referring to a report item in a subreport is not allowed.

But that is a bit old, there is also this more recent discussion of work-arounds, provided by Microsoft employee and a MS BI MVP:

You are going to need to replace the subreport item with a data region like list, table, or matrix to be able to get the proper reference you are looking for.

[Skipping down to another post]...

Now, it seems you want to calculate the difference between main report and the subreport. Also, because they have the different data source, so you cannot use nest table/matrix/list, right? If so, one workaround I can think of is pass parameter to the sub report and calculate the total/subtotal in sub report. I mean, create several hidden/ internal parameters, pass the values from main report to sub report through parameters and then calculate the total/subtotal there.

Jeroen's answer to the linked question point towards the direction I would go: use a "Shared Dataset" and enable caching if the dataset is slow to execute. The same dataset execution can then be used for the parent and subreports. This can change the use of parameters: they usually get moved from the SQL query to the filter of the Dataset in the report.

But with the Lookup function introduced in SSRS 2008R2, you can get very flexible with report level joins between datasets.

The details of how I'd design this depend a lot on how much other data needs to get passed back and forth, and how neatly the queries for the reports can be knit together.

查看更多
登录 后发表回答