I have an SSRS report with information about invoices my company has issued with things like amount, date, payment, amount due, etc. I'm using SSRS 2008 (not R2) in Visual Studio 2008 environment. My issue has to do with formatting the report using the Expression editor. Currently, an invoice will be formatted as silver if the invoice has an Amount Due (a column) over 0.01 (outstanding invoice). We also issue credits (negative amounts) and these are almost always the negative amount of a previous invoice.
So, an invoice that has a credit issued to it will still show as silver because it's amount due > 0.01. But if there is a credit issued to this invoice, it's not actually outstanding and should be white. For example, if an invoice is $100.00, and there is a credit after for ($100.00), the original invoice's background color should be switched to white.
Here's where the code explanation comes in. I thought this was possible with custom VB code in the report, but it seems like the Expression editor in SSRS will not recognize my function as it says "Unrecognized identifier". I googled this a bit, and most of the topics I came across said that it will show that but actually work anyway. Well, I'm pretty sure it's not working at all, because I put this in as my expression and got all white cells for a certain column:
=IIF(Fields!Amount_Due.Value > 0.01,
IIF(Code.HasCredit(Fields!Amount_Due.Value) = True, "Blue", "Silver"), "Red")
The HasCredit function is below.
Function HasCredit(ByVal currentAmt as Double) As Boolean
Dim i as Integer
Dim amt as Double
Dim amts as System.Collections.ArrayList = New System.Collections.ArrayList()
Dim negativeAmt as Double
Dim retValue as Boolean = "False"
i = 0
For i = 1 to Report.Parameters!Test.Count()
amt = Report.Parameters!Test.Value(i)
amts.Add(amt)
Next
negativeAmt = currentAmt * -1
If amts.Contains(negativeAmt) Then
retValue = "True"
End If
Return retValue
End Function
When these two pieces are run, I get white background for all cells of this column. I read something on the net saying that only Shared functions would work, but I found multiple other examples showing functions that were not shared that worked. When I make it shared, it gives me: BC30369 Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class. due to the Report.Parameters!Test.Count() line. I got the idea for using Report.Parameters from http://social.msdn.microsoft.com/Forums/en-US/sqlreportingservices/thread/a7d59224-0ee5-491e-883b-2e5fcb3edeab.
So to reiterate, what I'm basically trying to do is get the values of each cell in this column into a collection and, for any two amounts where one amount has a negative equivalent, give it a white background instead of silver.
In SSRS 2008 R2, even if you reference a valid custom code method, the expression editor may still warn that the identifier is invalid. This does not always mean that it's invalid. My function worked even though that warning was thrown.
After a bit more researching I figured out how to solve this. I basically had to add a textbox with the =Join() function so that all the values in a column were put in this textbox; then reference the textbox in custom code and use a boolean value in the expression editor. Detailed instructions below.
1) Add a multi-valued parameter to your report (Right click Parameters, Add Parameter). Name it, select Allow Multiple Values, select Hidden for parameter visibility. For Available Values tab, select Get values from a query. Point to your dataset, and set the Value field to the column you want the parameter to check. For me that was my Amount Due column. Label field is irrelevant and can be left blank. In the Default Values tab, do the same, make sure the value field is set to the same column as before. Under Advanced, select Never refresh.
2) Create a new textbox on your report. Might want to name it, ex txtColumnValues. Edit the expression and put this in: =Join(Parameters!YourParameter.Value, ",") This will get all of the field values from the column you specified in your parameter, each separated by a comma.
3) Edit your report's custom code and make a VB function (as Boolean) to check the textbox. For example here is my code.
Public Function HasCredit(Amt as Double, ri as ReportItems) as Boolean
Dim retValue as Boolean = False
If Amt > 0.00 AndAlso ri!txtAmounts.Value.Contains(Amt*-1) Then
retValue = True
End If
Return retValue
End Function
4) Finally go to your expression editor for the field you want to change. In my example I wanted to change the background color if the current field had a negative equivalent in one of the other fields (my textbox), so my code looked like this:
=IIF(Fields!Balance.Value > 0.01
AND Code.HasCredit(Fields!Balance.Value, ReportItems) = False, "Silver", "White")
I had to take separate pieces of info from 3 or 4 pages, put them together, and hope they worked... after about a week they did... I guess it's all about persistence. If you need any further help with this just let me know.
Related
I am currently trying to work on fixing some cascading parameter issues I am having.
We recently put in a fix to a few SSRS reports that help with rerunning cascading parameters when the parent parameter is changed, if it helps, here is the instructions we followed to implement this: Cascading Parameters.
The main structure for most of our reports works like this:
#Hierarchy, select whether you want to filter by "Market" or "Region"
#HierarchySelection, select which markets or regions you want to filter by
#PropertyInternal, gets used to refresh #Property automatically each time #HierarchySelection is changed, this is populated by a SQL dataset
#Property, select which properties you want within those given markets or regions, the defaults are set up as Parameters.PropertyInternal.Value and get refreshed each time #HierarchySelection is changed
In order to get #Property to refresh properly, we have to feed #PropertyInterval with a NEWID() so it sees it as a new value each time it runs, so the value is CONCAT( Property, "|", NEWID() )...Property is always a 4 digit character
The issue I am having is that now is that subscriptions are failing because the Property parameter gets refreshed and unselects the values. Or if I have to feed it values, I can't exactly feed the NEWID() as I am unaware of what the value will be when the report is run.
My only thought is to try and remove the GUID from #PropertyInternal and feed that value to #Property instead, but either that is not possible or I just cannot figure out the syntax.
Any one have any better ideas for how I am handling this or if there is a way to remove the GUID?
I am essentially looking for something similar to this if possible, but getting the error below it.
=SPLIT(LEFT(JOIN(Parameters!PropertyInternal.Value,","), 4),",")
The report parameter 'Property' has expression-based ValidValues. The sizes of the value and the label (multi-value) arrays have to be identical. (rsInvalidValidValueList)
Public Function CommaSeparatedParam(ByVal strArray As Object()) As String
Dim returnStr As String = String.Empty
For Each str As String In strArray
returnStr = returnStr + Mid(str, 1, InStr(str, "|") - 1) + ","
Next
Return Left(returnStr, Len(returnStr) - 1)
End Function
As the subject expresses, I'm trying to sum the values of a string field where spaces may exist. It must be done this way, unfortunately.
The database is very old. The original developer chose to make all fields Text fields; to get over the null value problems, a function was written in VB6 to replace any null value with a space. This cannot be changed.
Fast forward to now, I'm trying to create a report that sums the length field without changing spaces to nulls first, and it should be done entirely through the control source property of the report.
I've added some of what I've tried below, but every time the report is run, I receive:
Data Type Mismatch
...and I'm not sure how to get around it.
Ideally, I'd like to keep the users out of the database completely, and just add a combo box that lists the reports created in the database so they can be opened by name without having to run any additional update queries first.
=Sum(IIf([MY_LEN]<>" ",DCount("[MY_LEN]","MY_TABLE"),0))
=Sum(Nz(Iif(Trim([MY_LEN])='',Null,[MY_LEN]),0))
=DSum("[MY_LEN]","[MY_TABLE]","[MY_LEN]<>' '")
=Sum(Iif(Val([MY_LEN])>0,[MY_LEN],0))
=(SELECT Sum([MY_LEN]) AS MyLen FROM MY_TABLE WHERE (((MY_TABLE.[MY_LEN])<>' ')))
Is this possible?
Can't compare anything to Null. Can't say If x = Null Then because Null is undefined. So you can't test if undefined = undefined. Use If IsNull(x) Then in VBA and Is Null in query criteria. Don't really need IIf() for Sum() aggregate, other aggregates such as Count or Avg would.
To handle possible space, empty string, or Null for a text field holding numeric data.
=Sum(Val([MY_LEN] & ""))
I have an Access form with a textbox bound to a currency field in a table. As expected, anything other than a numerical entry generates an error. Occasionally, users need to enter several amounts and have those added together and the result entered into the currency field.
To accomplish this, I would like users to enter an equal sign followed by a valid arithmetical string which would evaluate to a number exactly as they would in an Excel cell. For example, if a user enters "=5.31+2" I want the field to evaluate to "7.31" and use that as the value passed to the table when the record is updated or saved. The current workaround is to use the Calculator application but that isn't the ideal solution.
I tried the following code and applied it to both the BeforeUpdate and OnLostFocus events of the textbox (named "tbxTotal_Paid") but neither worked. I simply got "The value you entered is not valid for this field" error.
Dim charCt As Integer
Dim evalStr As String
If Left(tbxTotal_Paid, 1) = "=" Then
charCt = Len(tbxTotal_Paid)
evalStr = Right(tbxTotal_Paid, charCt - 1)
Me.tbxTotal_Paid = CCur(evalStr)
End If
Is this simply applying the code to the incorrect event or is this a coding issue? Any assistance is appreciated.
For me your code looks fine but you might put it in the wrong place.
Like you said Acess is giving you this error because the textbox is bound to the currency-field. So it will never accept non-numerical values because the value-checking code fires even before the before_update-event.
I think the best solution would be to hide your bound text box using Me.tbxTotal_Paid.Visible = False and creating a surrogate textbox which is not bound. You put your code in the beforeUpdate-Event or Change-Event of your surrogate. At the end you should check your final result with IsNumeric(). That way your surrogate textbox writes only correct values to your bound hidden textbox and only numbers arrive at your table.
An alternative would be to change the currency column to a string-field but this would not be wise because of potential wrong data in your database.
I have a access 2007 report for which one of the fields returns a numerical value of $0 if there no dollar amount associated with the project listed, else it returns a dollar amount greater than $0.
What I want to do is build the report so that if the field value = $0, I want it to read "TBD" (without the quotes) else, I want it to return the dollar amount.
Two part question--first, what Sub class would I write the code under?
And, how would I go about writing the if else statement?
Here is what I came up so far and it doesn't do anything (no errors, just returns the report with the $0's printed where no dollar amount is available).
Private Sub Cost_AfterUpdate()
If ([YearofExpenditureCost] >= 0) Then
Me.Cost = "TBD"
Else: Me.Cost = [YearofExpenditureCost]
End If
Where the [Cost] is the name of the text box containing the ControlSource value of [YearofExpenditureCost].
Many thanks in advance for any advice you can offer.
You can use an immediate If as the Control Source of a textbox on the report. Ensure that the testbox has a name other than any of the fields in the recordsource:
=IIF(YearofExpenditureCost=0, "TBD",YearofExpenditureCost)
The format property of the form has four parts (from Access help):
First The format for positive numbers.
Second The format for negative numbers.
Third The format for zero values.
Fourth The format for Null values.
Thus, if you set the format property to ;;"TBD"; it should display the value or "TDB" if it's zero -- there is no code required for this, just setting a property of the control on the report.
I have a textbox in my SSRS 2005 report. The expresssion for this textbox is:
=IIF(IsDBNull(Fields!fOrgID), Code.SetMyVar("null"), Code.SetMyVar(Fields!fOrgID.Value))
I have also tried IsNothing(Fields!fOrgID) and a few other variations of checking for nulls.
I have modified the SetMyVar function for testing and it now looks like this:
Public Function SetMyVar (var as String)
MsgBox(var, VbOKCancel, "Test1")
If var Is Nothing Then
Return "NOTHING"
Else
MyVar = var
Return var
End If
End Function
I also have the public variable MyVar:
Public Shared Dim MyVar as String
When my database query returns data, this correctly evaluates, a messagebox is displayed with the value, the textbox gets set with the value, and the world is generally a happier place.
When my database query does not return a value though, I get the error:
The query returned no rows for the data set. The expression therefore
evaluates to null.
and the SetMyVar function never appears to be ran (you never get the messagebox popup). As expected, my emotions range from anger, sadness, and bitter hatred of SSRS.
I read something about SSRS evaluating both sides of an IF statement, so perhaps that is why I get the error (likely then on Code.SetMyVar(Fields!fOrgID.Value))... not sure how I get around that though.
Thoughts? Suggestions? Words of comfort?
From the sound of things, it seems likely that the issue is that SSRS is having a problem displaying zero records. I'd recommend one of the following:
1) Use a control that handles zero records appropriately (Tables do. I think Lists do as well).
2) Modify your query to return a single record with blank values if it would otherwise return zero records.
An answer to the original question:
=IIF(IsNothing(Fields!fOrgID),
Code.SetMyVar("null"),
Code.SetMyVar(IIF(IsNothing(Fields!fOrgID),"Foo",Fields!fOrgID.Value)))
The error was from both sides of IIF being evaluated. The extra IIF in the statement above will avoid Code.SetMyVar from ever being called with a null value.
I believe you're right about about Iif always evaluating both of its value arguments (at least, it does in Visual Basic). I'm not sure why you're getting this precise error (unless strings can't be assigned a value of DBNull?), but you almost certainly want to attack this problem with a different method.
The reason for this is that your current code will likely always call both set methods regardless of the conditional value.
Formula that worked for my SSRS 2008 reports.
=IIf(String.IsNullOrEmpty(Fields!NullableFieldwithPossibleBlankStrings.Value),"Yes","No")
I tried this too (also tried a version with IsNothing)...
=Code.SetField(IsDBNull(Fields!fOrgID))
And changed the function to be one that accepts a boolean. I figure this above function would always return a true or false, but in the event of a NULL, I again get "The query returned no rows for the data set. The expression therefore evaluates to null.".
I need to pass back to my code if the field is null or not (as this will let me know if the datasource is null or not).
Let me know if you can think of a better way because I cannot.