【问题标题】:SSRS Group Variables SUM & VB.NET Custom CodeSSRS 组变量 SUM 和 VB.NET 自定义代码
【发布时间】:2020-04-14 03:45:58
【问题描述】:

我有一个 SSRS 报告,它使用组变量和执行字段计算的表达式。组变量使用查找、算术和逻辑运算在 3 个数据集之间提供正确的结果。

我需要对 tablix 页脚中的变量文本框表达式的结果求和。但是变量不会以这种方式工作,当我在 tablix 页脚中尝试不同的表达式时,我会出错。

所以我在线搜索了求和组变量,然后我来到了自定义代码解决方案,它使用 VB.NET 变量和函数来聚合然后显示值。但是自定义代码不太好用。请参阅页面底部的自定义代码。以下是我观察到的一些问题

自定义代码问题

  1. 如果我使用变量作为 Public Dim,导出到 excel 时总值变为 0(例如,屏幕上的“1,214,284”;导出到 excel 时为“0”。)
    • 如果我将变量声明为 Public Dim 更改为 Public Shared Dim,那么屏幕上的值是相同的,它们将导出到 excel。
    • 问题是 Public Shared Dim 似乎在 Visual Studio 中运行良好。但是在报表服务器上执行时,每次执行报表时变量都会累加(即ExecEvent#1:屏幕上的“150值”&excel;ExecEvent#2:屏幕上的“300值”&excel;ExecEvent #3:屏幕和 excel 上的“450 值”)。

有什么帮助吗?如何使这些值聚合和导出?如何让自定义代码 VB 变量正常运行。特别是服务器上的变量初始化正确设置和重置。

自定义代码更正说明

  • 在“添加”函数中,我添加了return thisValue 以解决细节变量值带有空白(不打印)的问题

参考文献

SSRS Summing Group Variables outside of the group

SSRS code variable resetting on new page

https://social.msdn.microsoft.com/Forums/sqlserver/en-US/d4a3969a-f3fc-457b-8430-733d7b590ff6/use-reportitems-to-sum-all-textboxes-in-a-tablix?forum=sqlreportingservices&prof=required


TablixDesign-WithGroupVaraibles

  • 组行,组页脚行

Tablix 页脚:组外的组变量表达式错误

  • 注意:这些表达式是不允许的

  • Variables!varExpenditureLifetime.Value --错误:表达式只能引用在相同分组范围、包含分组范围或报表上声明的变量.变量名称中的字母必须使用正确的转换。

  • Sum(Variables!varExpenditureLifetime.Value) --错误:变量不能用于聚合函数

报告代码:

组变量(“varLWSBegin_LifetimeExpense”)

= Code.addLWSBeginLifetimeExpense(CDbl(
IIF(Parameters!boolUseLwsBalance.Value = false, 0,
Lookup(Fields!ProjectId.Value, Fields!ProjectId.Value, Fields!LWSBegin_LifetimeExpense.Value, "dsCAPEXAmountsCustomDataUpload"))
))

Tablix 组行

Variables!varLWSBegin_LifetimeExpense.Value

Tablix 页脚行

Code.getTotalLWSBeginLifetimeExpense()

VB.NET 自定义代码

' 
' Add group variable values to an external variable declared on the VB Code. 
' In Tablix Group Properties variable the value will be the call to the addValue function 
' Then on your textbox you call the getValue function: 
' Group Var: Code.addValue(Fields!my_field). 
' Textbox:   Code.getValue() 
' 
' variable1, addTotalBudget, getTotalBudget
' variable2, addLWSBeginLifetimeExpense, getLWSBeginLifetimeExpense
' variable3, addExpenditureLifetime, getExpenditureLifetime
'

'TEMPLATE
Public totalMyField2 as Integer
Public totalMyFieldName as Integer

Public Function addMyFieldName(ByVal thisMyFieldName AS integer)
    totalMyFieldName = totalMyFieldName + thisMyFieldName
End Function

Public Function getMyFieldName()
    return totalMyFieldName
End Function

'MyVariables
Public Shared Dim totalTotalBudget as Integer
Public Shared Dim totalLWSBeginLifetimeExpense as Integer
Public Shared Dim totalExpenditureLifetime as Integer

Public Shared Function Initialize() 
    totalTotalBudget = 0
    totalLWSBeginLifetimeExpense = 0
    totalExpenditureLifetime = 0
End Function

'Functions
Public Function addTotalBudget(ByVal thisValue AS Integer )
    totalTotalBudget = totalTotalBudget + thisValue
    return thisValue
End Function

Public Function addLWSBeginLifetimeExpense(ByVal thisValue AS Integer )
    totalLWSBeginLifetimeExpense = totalLWSBeginLifetimeExpense + thisValue
    return thisValue
End Function

Public Function addExpenditureLifetime(ByVal thisValue AS Integer )
    totalExpenditureLifetime = totalExpenditureLifetime + thisValue
    return thisValue
End Function

Public Function getTotalBudget()
    return totalTotalBudget
'   Dim myval as Integer = totalTotalBudget
'   totalTotalBudget = 0
'   return myval
End Function

Public Function getTotalLWSBeginLifetimeExpense()
    return totalLWSBeginLifetimeExpense
'   Dim myval as Integer = totalLWSBeginLifetimeExpense
'   totalLWSBeginLifetimeExpense = 0
'   return myval
End Function

Public Function getTotalExpenditureLifetime()
    return totalExpenditureLifetime
'   Dim myval as Integer = totalExpenditureLifetime
'   totalExpenditureLifetime = 0
'   return myval
End Function

'<END>

【问题讨论】:

  • 我将 Public Shared Dim 变量粘贴到 VS2019 中,Dim 字就消失了。
  • 更新-为了防止变量值随着每次报告执行而增长,我在报告标题的文本框中调用了 Code.Initialize() 函数。页脚总计数据在屏幕上很好,但是当我导出报告时,即使变量声明为 Public Shared,页脚总计值也设置为“0”。
  • 更新 - 我尝试将变量声明为 Public Shared、Public、Dim、Protected Shared、Private。我还尝试如上所述初始化变量。当报告导出到 excel 取消变量值时,似乎发生了一些事件。这是预期的行为吗?有没有办法使用 SSRS VB 代码来累积值?

标签: vb.net reporting-services ssrs-2016


【解决方案1】:

能够像您尝试的那样使用变量的最简单方法是使用Report Variables 而不是组变量。如果您使用报表变量,则可以从任何范围访问它。

您可能需要稍微更改您的数据集,以便在报表级别计算聚合时明确无误地分组。我无法真正指导您,因为我不像您那样了解您的数据。但作为到达聚合范围的更简单途径,check this SO question。您可以将表组用作聚合函数的范围。

我不得不按照您想要完成的工作来做一些事情,而报告变量为我提供了帮助。让我知道这是否适用于您的场景。

【讨论】:

  • 感谢范围计算的参考。我以后可能需要参考它。但是在这一点上,我放弃了自定义代码并将所有数据放入一个数据集中以使 SUM 工作。梅西耶,但它有效。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多