SSRS reference report variable from report function - function

In SSRS, is it possible to reference a report variable from a report function?
Below is my report function. Instead of declaring and setting the MaxRFWIDRange in the function, I'd rather reference a report variable with the same name.
Function ValidateRFWIDRange(FromRFW As Integer, ToRFW As Integer) As Boolean
Dim DiffRFW As Integer
Dim MaxRFWIDRange As Integer
MaxRFWIDRange = 10000
DiffRFW = ToRFW - FromRFW
If DiffRFW > MaxRFWIDRange Then
Return False
Else
Return True
End if
End Function

It looks like you can do this.
I tested this by adding a variable TestVariable to a report, then adding the following custom code:
Function TestVariableFunctionality(var as Microsoft.ReportingServices.ReportProcessing.OnDemandReportObjectModel.Variable) As String
Return var.Value
End Function
I called the function from a textbox like so:
=Code.TestVariableFunctionality(Variables!TestVariable)
This outputs the variable's value in the textbox as a string.
You must declare the full namespace as Microsoft.ReportingServices.ReportProcessing.OnDemandReportObjectModel.Variable, because Variable alone is not defined. See below for more info.
Accessing variable in SSRS function
From an MSDN blog:
Assume that you've created a report variable named "Reportvariable1".
All you need is a piece of custom code.
Public Function SetVariableValue(val as Microsoft.ReportingServices.ReportProcessing.OnDemandReportObjectModel.Variable)
val.Value = val.Value + 2
End Function
As simple as that. All i'm doing in the function is, take the report variable reference, add 2 to the existing value. That's it.
How you can call this function from your report item?
=Code.SetVariableValue(Variables!Reportvariable1)
This will do the magic. Also note that in the above expression i'm just passing the variable reference and not its value.

Related

Multi-value parameters in ssrs

I have a multi-value parameter. how I get one by one values from this parameter.
value=new_index and label=new_french
and want to insert these values into these labels
You can access the individually selected multi-value parameters by their index (the index is zero-based). So if you want the first selected parameter value (for example, to put it into a label), you can address it like so:
=Parameters!MyParameter.Value(0)
You could access them all using custom code:
Function DoSomething (ByVal parameter As Parameter) AS String
Dim Result As String
If parameter.IsMultiValue then
For i As integer = 0 To parameter.Count-1
Result = Result + CStr(parameter.Value(i)) + ", "
Next
Result = Left(Result, Result.Length - 2)
Else
Result = CStr(parameter.Value)
End If
Return Result
End Function
then use this expression to access the result:
=Code.DoSomething(Parameters!MyParameter)
Note that you are passing the parameter object here, not the Value property. We access the Value property in the custom code function.

NOT IN in SSRS TextBox

How can I write NOT IN in TextBox expression?
I must check if some field value not belong to some list of strings, and then do some work.
Example:
Iif(SomeField.Value NOT IN ('Text1', 'Text2'), DoSomething, nothing)
I wrote code like this and got error when previewing report, and error was :
Overload resolution failed because no accessible 'Iif' accepts this number of type arguments
How can I do this stuff?
Try this small piece of custom code that accepts a string array. Just paste it into the report code section of the report..
Public Shared Function ValueExists(ByVal arr() as string, checkVal as string)
Dim i As Long
For i = LBound(arr) To UBound(arr)
If arr(i) = checkVal Then
return true
Exit Function
End If
Next i
return false
End Function
Usage would involve splitting the string into an array using the Split function
like so:
=iif(Code.ValueExists(Split("Your,comma,separated,string,in,here",","),"StringYouWantToFind")
,"Your value exists"
,"your value does not exist")
You can simply write the code like this:
Iif(SomeField.Value <> 'Text1' AND Field.Value <> 'Text2' , DoSomething, nothing)
I got this one in one report:
=iif(join(Parameters!Parameter1.Value,",") like "*" & Fields!Field1.Value & "*","Color1","Color2")
This instruction helps me to determine the fill colour of a cell inside a tablix, where:
Parameter1 is a multivalue parameter.
"Join" lets me have a string with all selected values from a multivalue parameter, eg. "value1,value2,value3,value4"
Field1 is the field that contains the values filtered by Parameter1
Color1 is the color if the value of the cell is included in the selection of parameter
else Color2
works well

How to return a range object from a user defined function in vba

I have this piece of code in excel:
Private Function RelCell(NmdRng as String) as Range
Set RelCell = Range(NmdRng).Cells(1,1)
End Function
it gives the runtime error "91': object variable or with block variable not set.
I really don't know what is the problem with my function.. someone does?
I don't know if this is the problem but your are only setting the range and aren't returning anything from the function.
Try declaring a range variable with a different name as the function and return that.
Actually, you should be able to return a range from a UDF as described in this MSDN Thread.
Here is the code given by the MVP:
Function GetMeRange(rStartCell As Range, lRows As Long, iColumns As Integer) As Range
Set GetMe = rStartCell.Resize(lRows, iColumns) ' note the use of Set here since we are setting an object variable
End Function
(and it works)
Tiago's comment points out a very right thing, as you want to access a named range, it should be defined first.
You can try to set a breakpoint in your UDF and see if the Range(NmdRng) is defined.
Your named range already has a cell reference attached to it, so you shouldn't need to have the .Cells(1,1) at the end of it.
Using the .Range(nmdRng) property alone will return the range object you are looking for.
Try:
Private Function RelCell(NmdRng as String) as Range
Set RelCell = Range("NmdRng")
End Function
Please rewrite your code and test it as follows :
Private Function RelCell(NmdRng as String) as Range
Dim TestRange As Range
Set TestRange=Range(NmdRng)
TestRange.Activate 'I think that error will occur here because, NmdRng is somehow invalid
Set RelCell = TestRange.Cells(1,1)
End Function

VBA function call

Is there a way to call a function, where the call is stored in a table
**Record 1 task Function call**
124567 Email customer Call function emailcus(a,b,c,d)
434535 AddCost Call function addcost(a,b,c,d)
Cheers
Graham
Yes, you can use the Eval() function for that.
Syntax:
Dim ReturnValue As String
ReturnValue = Eval("MyFunction(1, 2)")
Note that you have to provide the exact function call including parameters.
I'm pointing this out because I'm not sure if the parameters a, b, c, d in your example are only dummy values for your example, or if you expect VBA to fill in the values of some variables named a, b, c, d automatically.
The Eval function does not do this, so if you need variable values as parameters, you would have to do something like this:
Dim ReturnValue As String
Dim EvalString As String
EvalString = "MyFunction(" & Variable1 & ", " & Variable2 & ")"
ReturnValue = Eval(EvalString )
This is a variation on the answer already given by haarrrgh, so if you find it useful be sure to upvote that one as well.
There's another way to deal with placeholders in your DB-stored function calls. First, change your data thusly:
**Record 1 task Function call**
124567 Email customer Call function emailcus([TokenA],[TokenB])
434535 AddCost Call function addcost([TokenA],[TokenB])
Note that the [SquareBrackets] are not actually required syntax in this example, just something that I tend to use in this situation. The important part is to make the parameter tokens something that doesn't appear elsewhere in the string value (including other tokens). You can use as many parameters as you need, just make sure that the calling code knows about how many are expected by each function-call string (I cut it down to shorten my following code).
Then when it's time to call your function, do this:
Dim ReturnValue As String 'or as appropriate for individual the function's return
Dim EvalString As String
EvalString = 'code to fetch from table
EvalString = Replace(EvalString, "[TokenA]", strValueA) 'strValueA passed in?
EvalString = Replace(EvalString, "[TokenB]", strValueB) 'strValueB passed in?
ReturnValue = Eval(EvalString)
In VB6, at least (so I assume it's true in VBA), Replace is faster than concatenation. I also find this more readable, but that may be because I'm used to it from using a similar technique to build SQL commands in code (using Const declarations rather than DB storage, but that would work too).
EDIT
As I reread my "finished" post just after submitting it, I realized that there's a gotcha lurking in there. Because you're doing substitution before submitting the string to Eval, these are actual values that are being put into the string, not variables. The code I presented above works fine if your parameters are numeric, but if they're String type you have to include the quotes, either in your data or in your Replace call. I prefer the former, so change your data to this:
**Record 1 task Function call**
124567 Email customer Call function emailcus('[TokenA]','[TokenB]')
434535 AddCost Call function addcost('[TokenA]','[TokenB]')
This works as tested with a Const. Stored in a DB record, you might need this instead:
**Record 1 task Function call**
124567 Email customer Call function emailcus(""[TokenA]"",""[TokenB]"")
434535 AddCost Call function addcost(""[TokenA]"",""[TokenB]"")
(which also works with a Const...).
The alternative is to leave the data as it is in my first part, & change the Replace calls:
EvalString = Replace(EvalString, "[TokenA]", """" & strValueA & """") 'strValueA passed in?
'or maybe
EvalString = Replace(EvalString, "[TokenB]", "'" & strValueB & "'") 'strValueA passed in?
A couple of other potential gotchas: These must be Functions, not Subs, and they must be declared Public in a module, not in a Form's code.

Populating multiple values in rdlc reporting

I am using rdlc report, i have a column in database which i want to display in the report.
vehicleDamageArea=1,2,3
In the report I need to mark the placeholders with these values.
=iif((Fields!vehicleDamageArea.Value="3"),Chr(253),Chr(168)) like this.
But as we know,it will check the whole value 1,2,3="3" not the splitted values.
Any suggestion to check by splitting the vehicleDamageArea parameter.
I made it to work as below
Public Shared Function CheckValue(ByVal InString As String,ByVal input as String) As Char
Dim output As String = String.Empty
Dim Parts() As String = InString.ToString().Split(",")
For i As Integer = 0 To Parts.Length - 1
If Parts(i) = input Then
output = Chr(0120)
Exit For
Else
output = Chr(0111)
End If
Next i
Return output
End Function
You can get the individual values using the split function in reporting services. It returns a zero-based string array, so for your example you need
=Split(First(Fields!ID.Value),",")(2)
You should make a function that accept a comma separated expression, than process this string and return a Boolean, then call this function as for boolean value.