SSRS Calculated field with IIF expression gives #error when the condition is false - vb.net

Hello I'm fairly new to SSRS and I'm working on making my first report for work. It has been going fine until today when I needed make a textbox in Tablix handle both a numbers calculation and text. A column in my SQL table that was previously a purely number field now has n|a for certain rows. So I wrote the following expression to handle those n|as. When the proceeds field is numeric the formula works, but when it's an n|a it shows up on the report as #error and I can't figure out why. I've removed all formatting from the textbox to the same result.
=IIF(Fields!Proceeds.Value<>"n|a",Fields!Proceeds.Value / Fields!DealBalance.Value,"n|a")

As TnTinMn explained, IIF always evaluates both the true and false part of the expression.
The simple fix is to do something like
EDIT Revised due to update from OP
=IIF(Fields!Proceeds.Value<>"n|a",VAL(REPLACE(REPLACE(Fields!Proceeds.Value,",",""),"$","")) / Fields!DealBalance.Value,"n|a")
This simply strips out the $ symbol and commas and then converts the text to a number using VAL(), if the text is not a number it will return zero which will not cause an error as the false part of your expression would give 0/DealBalance.

Related

Why is the output for my datediff expression to return a number of days bracketed?

I have written the following expression to give me the number of days between a date and today.
=IIF(Fields!First_Check_Start_Date.Value = nothing,nothing,datediff("d",Today,Fields!First_Check_Start_Date.Value))
The output is correct but is bracketing the number
ie. (292) instead of 292
Any help as to why this is
if you are using text box to display your result, check what is the type of your text box. Make it as number and look for it's format and set to correct
Else you could also format like below
=IIF(Fields!First_Check_Start_Date.Value = nothing,nothing,Format(datediff("d",Today,Fields!First_Check_Start_Date.Value),"##"))
So it appears that whilst viewing in SSRS Report builder the numbers are bracketed (292), however once export to either csv or excel the bracket disappears and is replaced bi a minus -292 which is correct
Thanks

How to Fix #Error When Calculating a DateDiff With an IIF Statement in SSRS

I'm creating a report in Reporting Services to display time-spans for classroom sessions in our program. One column that was requested is to show the total number of hours between the start and end time of one session. I used a DATEDIFF function showing the DateInterval in hours as the total hours in one time-span, but I get a #Error returned for the blank values that haven't been assigned a begin or end time.
I added an IIF statement to mark the values that are blank as "TBA", but that didn't work. Since I'm casting a string to a datetime value, I enclosed the DATEDIFF statement in a CStr function, but that also isn't working. Lastly, I did the simple thing and set the text box to display a Number value since it's returning the total number of hours, but of course, that didn't work either. My code is as follows:
=IIF(Fields!BeginTime.Value = "", "TBA", CStr(DateDiff(DateInterval.Hour, Fields!BeginTime.Value, Fields!EndTime.Value)))
It's supposed to show that "TBA" value in the blank spots, but I still get a #Error. The total hours are coming up fine for the values that aren't blank, however. I checked the value in the dataset to see if it's returning a blank or a NULL-labeled value, and it is returning a blank value.
I believe the issue is arising in the conditional statement. If Fields!BeginTime.Value is a date datatype, comparing it to "" will likely result in the error you're getting. I suggest using the IsNothing function or just checking if the field is nothing. The two options are as follows.
=IIF(IsNothing(Fields!BeginTime.Value), ...
Or...
=IIF(Fields!BeginTime.Value Is Nothing, ...
With that said, you shouldn't need to use the CStr as DateDiff will return an integer value. Your textbox should be set to Default or some text variant to prevent errors there. The expression will return either an integer or string so a number format on the textbox will not work.
Additionally, if you're having the same issue when there is no EndTime, you may want to add an OR to the conditional to fix that.
=IIF(IsNothing(Fields!BeginTime.Value) OR IsNothing(Fields!EndTime.Value), ...

Conditional Formatting in Access with function in VBA

I'm familiar with programming in general but not with Access forms or VBA.
I have the conditional formatting working. Now I need to make it respond to the result of a custom VBA function.
Let's say there is a limit, based on the customer, to how many times something can happen.
The label will show the count, "Trip 3 of 5".
When we get to 5 of 5 it should be yellow and 6 of 5 should be red.
Note that I want the conditional formatting rules to be in Access, not in VBA. VBA just returns the value we test in the condition.
I don't see how to use the result of a VBA function in the conditional formatting expression, but it seems like you can use variables.
I tried
TripCountValid: GetTripCountValid(ID)
and then used TripCountValid in my condition, but that didn't seem to work.
GetTripCountMsg and GetTripCountValid are in the Orders module because that is where GetBillAcctMsg is, which I am emulating.
I could move it to the code-behind for the form.
My MsgBox in GetTripCountMsg gets called but not the one in GetTripCountValid.
A function can be referenced in Conditional Formatting. Use the Expression Is: option. Assuming your function returns a Boolean (True/False), the CF rule would simply be:
Expression Is: GetTripCountValid([ID])
Set the desired formatting to display if the function returns True. Or if you prefer to change display for False:
Expression Is: NOT GetTripCountValid([ID])
If those expressions are not explicit enough use = sign with whichever Boolean value you prefer.
Expression Is: GetTripCountValid([ID]) = True
If function returned something other than Boolean, use the last syntax to compare with whatever parameter appropriate for situation. The point is, the expression must evaluate to True or False.

SSRS syntax error with countdistinct iif

I have an SSRS report where I want to count specific results from the larger query (Overall Total - then of those total "Open" and total "Closed"). I'm trying to use the expression:
=CountDistinct(IIF(Fields!CaseStatusCode.Value='OPEN', Fields!CaseID.Value, Nothing))
But whenever I run the report I get:
There is a syntax error in the Value expression for the textrun ‘Textbox24.Paragraphs[0].TextRuns[0]’: ‘)’ expected.
I've added and removed multiple ) but the error persists. I've also tried the expression in different textboxes and the error continues (just changing the name of the textbox it's in). What am I missing?
Solved it. Apparently I am supposed to use " instead of '. Corrected code below.
=CountDistinct(IIF(Fields!CaseStatusCode.Value="OPEN", Fields!CaseID.Value, Nothing))

Using iff expression in rdlc report with null check keeps giving errors

In a rdlc report i'm using an expression to hide the row visibility.
If the expression is true the row is hidden.
The value of the field can be "Ja", "Nee" or Null. Only if it's "Ja" the row must be shown and the expression result should be false.
The expression
=Iif(Fields!Vleugel.Value Is Nothing,true,false)
is working but when I want to add the "Ja" condition like:
=Iif(Fields!Vleugel.Value Is Nothing,true,Iif(Fields!Vleugel.Value.Equals("Ja"),false,true))
it's causing an error like "Object reference not set to an instance of an object." when the field is null.
Can't figure out what's wrong since it's looks simular to examples I found.
If you use the Row Visilibily property you don't have to write an iif statement, it is enough to give the expression that will hide the row, try this:
=Fields!Vleugel.Value Is Nothing