简体   繁体   中英

SSRS Report Builder 3.0 - Why can't I use a code function as the source expression of a Lookup Function?

I've built a report in Report Builder 3.0 that has a couple of hundred cells that perform Lookup functions in my datasets. Obviously, with the number of cells, I would like to be able to create the expressions dynamically. I've been able to utilize the textbox names to do this with the following function:

Public Function TextBoxName(ByVal meString As String) As String
 Dim s As Integer = 19
 Dim e As Integer = meString.IndexOf(“_TextBoxExprHost”) – 19
 TextBoxName = meString.Substring(s,e)
 End Function

Expression:
 =Code.TextBoxName(Me.ToString)

The above expression works as needed everywhere in the report except when I want to use it in a source expression in a Lookup function. Here is an example:

Iif(IsNothing(Lookup((Code.TextBoxName(Me.ToString) & "R"),Fields!SECTION_STATUS.Value,Fields!LCTN_COUNT.Value, "Loads")),"0",Lookup((Code.TextBoxName(Me.ToString) & "R"),Fields!SECTION_STATUS.Value,Fields!LCTN_COUNT.Value, "Loads"))

The above example will compile but produces a #Error in the cell when I run the report. But, the report works as needed if I hardcode the textbox name for each of the source expressions.

"AP1010R" instead of Code.TextBoxName(Me.ToString) & "R"

I've found no examples of anyone using custom code to generate this source expression. Anyone have any ideas what could be going on here? Thanks in advance.

The problem is actually not with the custom code in the source expression (try a simple custom function that only returns a string and it works fine), but specifically with the Me.ToString portion. My guess is Me is trying to reference the wrong object while inside the Lookup.

I haven't found much detailed usage for Me , but a possible workaround would be writing your own custom lookup function which is detailed nicely in this MSDN forum post . The gist of it (using Lookup function terminology):

  1. Create a hidden, multi-value parameter
  2. Set available and default values to destination_expression column; set labels to result_expression column
  3. Add custom code:

     Function CustomLookup(id AS String) AS String Dim i As Integer i = 0 For i = 1 to Report.Parameters!YourParameter.Count() If Report.Parameters!YourParameter.Value(i) = id Then CustomLookup = Report.Parameters!YourParameter.Label(i) Exit For End If Next i End Function 
  4. Call it like: =Code.CustomLookup(Code.TextBoxName(Me.ToString) & "R")

This may not be feasible/performant enough (especially if your lookup dataset is large and/or destination and result expression columns change a lot in various lookups) but it could work well in some scenarios.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM