IIF statement with null value and multiple condition - reporting-services

I'm getting a problem with one of my expression in my report. It always gives me #Error and I think I know why but don't know how to set my expression.
Here's my expression :
=IIF(Fields!CMMTTEXT.Value.Contains("*Return*") AND Fields!SOPTYPE.Value = "INVOICE", "*** See return ***", "")
The Fields CMMTTEXT can be null sometimes and I'm getting an error on all of my rows except the one that has data in the CMMTTEXT field.
So I need a isnothing but I've tried couple ways of doing it but doens't seem to work
=IIF(IsNothing(Fields!CMMTTEXT.Value.Contains("*Return*")) AND Fields!SOPTYPE.Value = "INVOICE", "*** See return ***", "")
also this that I've seen on this forum :
=IIF(IsNothing(Fields!CMMTTEXT.Value) OR Fields!CMMTTEXT.Value.Contains("*Return*") AND Fields!SOPTYPE.Value = "INVOICE", "*** See return ***", "")
I have no idea at this point.
Thanks for the help!

The Contains function doesn't work with NULL values. Once it throws an error, the IsNothing function subsequently can't evaluate the results. Separating these out using the OR operator also doesn't work because it tries to evaluate both conditions even when the first one was true.
One way to work around this is to first check for NULL values and replace them with an empty string. Then use the Contains function on the results of this expression:
=IF(IsNothing(Fields!CMMTTEXT.Value), "", Fields!CMMTTEXT.Value).Contains("*Return*")

Related

SSDT report expression IIF/SWITCH

I am trying to create an expression that checks if the condition is not correct just go to the else statement, for an easy example I did the following
=SWITCH(TRUE, "AA", FALSE, "A"/0)
=IIF(TRUE, "AA", "A"/0)
as you may guess the conditions here were set always to print (AA) but the actual result is always (#Error), how to avoid this, I just wanted it to execute if true and ignore the error in the false section.
obviously, my statement is a bit more complicated but in short, that's the situation I am in with the following code
IIF(SPLIT(Parameters!JSON.Value, chr(34) & "Text" & chr(34)).Length > 1, SPLIT(SPLIT(Parameters!JSON.Value,chr(34) & "Text" & chr(34)).GetValue(1),chr(34)).GetValue(1), "")
when there is no value it should print an empty string but it looks like SSRS looks at the true condition and prints #Error regardless of the result of the IIF true or false.
how i can avoid this, or how to check if (#Error) and print something else?

Index was outside the bounds of the array in SSRS

I have two parameters , let's say P1 and P2. The sample expression I used for P2 is
IIF(P1.Label="string", "null" ,Split(P1.Label," ").GetValue(0))
When the condition is false, the split expression is working fine. But if the condition is true, I'm getting 'Index was outside the bounds of the array' error. If the condition is true, I need to pass the value "null" as varchar type.
Can someone please advice on this?
The problem with the IIF function is that it is a function not a language construct. This means that it evaluates both parameters before passing the parameters to the function. Consequently, if you are trying to do a Split on a parameter that can't be split, you will still get the 'Index was outside the bounds of the array' error, even when it looks like that code shouldn't be executed due to boolean condition of the IIF statement.
The best way to solve this is to create a safe string splitter function in custom code where you can use real language constructs. Also, check that the string is splittable by checking it contains a space instead of checking for a special string:
Public Function SafeSplit(ByVal SplitThis As String) As String
If InStr(SplitThis, " ") Then
Return Split(SplitThis, " ")(0)
End If
Return "null"
End Function
and then use this in your report for the Value expression instead of IIF:
=Code.SafeSplit(Parameters!P1.Label)

How can I hide specific duplicate rows in SSRS 2005?

Here's my problem: I have some some duplicate rows (rows with the same ID). What I want is to hide rows based on a specific's column value. The mentioned column is obviously not the ID column (it's the Description column).
I tried this on the Row Visibility (Expression):
=IIF (Fields!Incident_ID.Value = Previous(Fields!Incident_ID.Value)
AND
ReportItems!Description.Value <> "Incident Status Change to Work In Progress from Open", true, false)
So, I want to exclude the rows in which Description is not equal to 'Incident Status Change to Work In Progress from Open'.
The message that I get is:
The Hidden expression for the table 'table 1' contains an error:
Object variable or With block variable not set.
Any ideas on this?
Thanks in advance
I think you are probably getting that error because there are one or more rows in your query result that have a NULL value in the Description field.
You could test this theory by simplifying your expression to this:
=IIF(Fields!Incident_ID.Value = Previous(Fields!Incident_ID.Value), true, false)
just to see if you can run the report without error.
If that works, then you need to add a test for a NULL value as part of the larger condition test, to get back to the logic you intended.
Try this:
=IIF(Fields!Incident_ID.Value = Previous(Fields!Incident_ID.Value) AND IsNothing(Fields!Description.Value) = false AND Fields!Description.Value <> "Incident Status Change to Work In Progress from Open", true, false)
This should avoid the error by preventing the evaluation of the last part of the condition, when the Description has a NULL value.
I think you're on the right track just using ReportItems instead of Fields!
=IIF(Fields!Incident_ID.Value = Previous(Fields!Incident_ID.Value) AND Fields!Description.Value <> "Incident Status Change to Work In Progress from Open", true, false)
SSRS can be wonky sometimes. Try parenthesizing:
=IIF(((Fields!Incident_ID.Value = Previous(Fields!Incident_ID.Value)) AND (IsNothing(Fields!Description.Value) = false) AND (Fields!Description.Value <> "Incident Status Change to Work In Progress from Open")), true, false)

Combobox null in if statement

I am trying to code an if statement where if a certain combobox is null, then it runs a certain part of code if it has data in it then it runs another. I wrote up this:
Private Sub ProjectAddSetDateAutoBtn_Click()
If ProjectAddAllDueDateAutoCmBx = Null Then
'Code1
Msgbox("ComboBox Is Null")
Else
'Code2
Msgbox("ComboBox Has Data")
End If
End Sub
I leave the combobox with no data, and then it doesn't run the code in the first part of the if or the code in the 2nd part of it either! If I enter data into the box, it runs the 2nd part of the if statement perfectly. There are no errors, I am quite stumped on this. Do ComboBoxes have their own "Null"? Is there a problem with this if statement?
Nothing is ever equal to Null, not even another Null.
Use IsNull() to check whether the combo box is Null.
'If ProjectAddAllDueDateAutoCmBx = Null Then
If IsNull(ProjectAddAllDueDateAutoCmBx) = True Then
I would suggest
If IsNull(ProjectAddAllDueDateAutoCmBx.Value) Then
It correctly checks for Null (IsNull instead of = Null), and it explicitly checks the value of the combo box.
(In most cases -- depending on the context -- just using the name of the control yields the value, but it doesn't hurt to be explicit.)
You cannot use a = Null comparison to get the results you want because Null propagates. To see this in action, try:
? Null = Null
in the Immediate Window and you'll see that Null is returned. Use the IsNull function, which will return true or false as you would expect.
Private Sub ProjectAddSetDateAutoBtn_Click()
If IsNull(ProjectAddAllDueDateAutoCmBx) Then
'Code1
Msgbox("ComboBox Is Null")
Else
'Code2
Msgbox("ComboBox Has Data")
End If
End Sub
While the accepted answer is totally correct, I use a different approach:
If HasValue(ProjectAddAllDueDateAutoCmBx) Then
where the HasValue function is:
Public Function HasValue(v As Variant) As Boolean
If Trim(v & "") <> "" Then
HasValue = True
Else
HasValue = False
End If
End Function
This has the advantage of treating NULL and "" (or any pure whitespace) values the same, which is many times what you want with MSAccess controls. For example entering a value in a null-valued textbox and removing it again with backspace will result in a ""-value, not NULL. From a user-perspective this is mostly meant to be the same.
[The (v & "")-part is just a trick to force conversion to a string.]
the equivalent of null in VB is Nothing so your check wants to be:
If ProjectAddAllDueDateAutoCmBx Is Nothing Then
....
it hope helps.

Checking for null and a value in SSRS

I want to check a value in an SSRS report to make sure that it isn't null, first, and then that it equals a particular value. My expression is as follows:
=iif(IsNothing(First(Fields!RecordStatusFlagId.Value, "DataSource")),"",(First(Fields!RecordStatusFlagId.Value, "DataSource") = 1,"","DELETED"))
So if RecordStatusFlagID.Value is not null and it doesn't equal 1 write "DELETED". Otherwise write "".
However, this gives me the error:
Error 75 [rsCompilerErrorInExpression] The Value expression for the textrun ‘textbox21.Paragraphs[0].TextRuns[0]’ contains an error: [BC30198] ')' expected. C:\Reports\MyReport.rdl 0 0
which doesn't make sense since I've verified that all of my parentheses are closed and matched.
Is it even possible to use an expression as the second term in the IsNothing operator?
I needed to add an iif to the second portion of the IsNothing expression
=iif(IsNothing(First(Fields!RecordStatusFlagId.Value, "DataSource")),"",iif(First(Fields!RecordStatusFlagId.Value, "DataSource") = 1,"","DELETED"))
I think you could simplify that into a single iif() statement:
=iif(
First(Fields!RecordStatusFlagId.Value, "DataSource") <> 1
And First(Fields!RecordStatusFlagId.Value, "DataSource") Is Not Nothing
, "DELETED"
, ""
)