Question

I'm trying to write a custom function in Report Builder 3.0 that basically checks the values in a multi-value Parameter (they'll be 1, 2, or 3) and returns either "Bronze" (1), "Silver" (2) or "Gold" (3) depending on what the highest each row is. So...

Public Function BronzeSilverGold() As String
    Dim bronzeFlag As Integer = 0
    Dim silverFlag As Integer = 0
    Dim goldFlag As Integer = 0
    Dim numPledges As Integer = Report.Parameters!PledgeValues.Count()

    For index As Integer = 1 To numPledges
        If Report.Parameters!PledgeValues.Value(index) = 1 Then
            bronzeFlag += 1
        ElseIf Report.Parameters!PledgeValues.Value(index) = 2 Then
        bronzeFlag += 1
            silverFlag += 1
        ElseIf Report.Parameters!PledgeValues.Value(index) = 3 Then
            bronzeFlag += 1
            silverFlag  += 1
            goldFlag += 1
        End If
    Next index

    If goldFlag = numPledges Then
        Return "Gold"
    ElseIf silverFlag = numPledges Then
        Return "Silver"
    ElseIf bronzeFlag = numPledges Then
        Return "Bronze"
    End If
End Function

The bit that's vexing me is that when I run the report the cell just returns #Error. The problem seems to specifically be the "Report.Parameters!PledgeValues.Value(index)". If I run this:

Public Function BronzeSilverGold() As String
    Dim bronzeFlag As Integer = 0
    Dim numPledges As Integer = Report.Parameters!PledgeValues.Count()
    Dim index as Integer = 0

    For index = 1 To numPledges
        If (Report.Parameters!PledgeValues.Value(1) = 2) Then
            bronzeFlag += 1
        End If
    Next index

    return bronzeFlag

End Function

Then it successfully returns the value 8 (since the first value in the parameter is a 2). If I change line 7 from Report.Parameters!PledgeValues.Value(1) to Report.Parameters!PledgeValues.Value(index) the cell in the report returns #Error

No idea what I'm doing wrong - if anyone has any idea's they'd be much appreciated.

Was it helpful?

Solution

Your problem is that the multivalue parameter value collection is zero-based, not one-based. That is, the first item in the array is index 0 and the last is Count - 1. Consequently For index As Integer = 1 To numPledges throws an array out of bounds error.

You need to use:

For index As Integer = 0 To numPledges - 1

As an aside, Count is a property not a method so it should not have the brackets after it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top