我正在尝试将报告中经常需要的一些表达式封装到报告函数中,以便我可以编写一次代码并在报告中的任何地方使用它。 但我确实是一名 Pascal/C# 程序员,并且在 VB(A) 语法方面遇到困难......
但即使是这个微小的日期格式化功能也无法正常工作......
Public Function DateFmt (ByVal dt As DateTime) As String
Return IIF(IsNothing(dt), "", FormatDateTime(dt, 2))
End Function
基本上,如果数据库中的DateTime
字段为NULL
,我想显示一个空字符串(空单元格),否则使用“短日期”格式选项.
在单元格上,我尝试将其添加为单元格表达式,但是当我开始键入如下表达式时:
=Code.
智能感知启动,但我的函数没有显示在该列表中......嗯......我错过了什么?
Update: I noticed that while intellisense doesn't work, I can still put the expression
=Code.DateFmt(.....)
into the cell - but alas, it doesn't work.While adding the expression directly nicely suppresses
NULL
dates in display, using this shared function causesNULL
dates to show up as01.01.0001
again in my report. WHY?
另外:我需要一个函数,根据选择列表返回一个值 - 基本上是一个 SWITCH
语句。这里的代码正确吗?
Public Function GetGrp(ByVal grp As String)
Select Case grp
Case "Region"
Return fields!RegionId.Value
Case "Somedate"
Return YEAR(Fields!Somedate.Value)
End Select
End Function
该函数实际上会导致编译失败,并显示以下错误消息:
Error in line 7 of user-defined code: [BC30469] Reference to a non-public member requires an object instance
<罢工>?!?!?!?!? SSRS 到底想告诉我什么?
更新#2:
好的,弄清楚了这一点 - Fields
集合在自定义代码中不可用(不清楚为什么不可用 - 无论如何......)。
所以我需要将此函数更改为:
Public Function GetGrp(ByVal grp As String, ByVal fields As Fields)
现在该部分可以工作了
最佳答案
从函数返回值的 VBA 方法是将值分配给该函数名称;我认为没有 return
关键字。
这也可能是这些函数没有出现在智能感知列表中的原因。
尝试像这样定义函数:
Public Function DateFmt (ByVal dt As DateTime) As String
DateFmt = IIF(IsNothing(dt), "", FormatDateTime(dt, 2))
End Function
Public Function GetGrp(ByVal grp As String)
Select Case grp
Case "Region"
GetGrp = fields!RegionId.Value
Case "Somedate"
GetGrp = YEAR(Fields!Somedate.Value)
End Select
End Function
更新:
此外,SSRS 中的 VBA 代码更像是静态函数的集合,这意味着它们无法访问报告状态(例如参数/字段)。
我会尝试将两个 field!*
值作为 byval
参数传递给 GetGrp
函数。
注意:我没有 SSRS 或 Office 来测试此代码,因此如果出现问题请告诉我。
关于sql-server-2005 - 尝试让 Reporting Services 理解我的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14642095/