IIF keyword is not working well Expression (RDLC Report) - reporting-services

I am using the following expression in the RDLC report.
But somehow, I am not able to use it very well, the values is returning "#Error" in the Report.
=IIf(Fields!AUGNA.Value=0.00,CSTR(Fields!AUGTOT.Value) & "%",CSTR((Fields!AUGTOT.Value/Fields!AUGNA.Value)*100) & "%")
but when I replace the false part with some string it works absolutely fine.
=IIf(Fields!AUGNA.Value="0.00",CSTR(Fields!AUGTOT.Value) & "%",
"REPLACED!")
All my fields in dataset is Decimal Type.i.e 72.88, 0.00 etc. I have tried to replace the false part with another IIF
=IIf(Fields!AUGNA.Value="0.00",CSTR(Fields!AUGTOT.Value) & "%", IIF( Fields!AUGNA.Value <> "0.00",CSTR((Fields!AUGTOT.Value/Fields!AUGNA.Value)*100) & "%",nothing))
but that also doesn't work!
Please Help!
Imran.

You have also put in double quotes the "test" part: Fields!AUGNA.Value="0.00"...
If you look better at your posted code, you will see that in the second version, you have replaced not only the "false" part, but also the "test":
=IIf(Fields!AUGNA.Value="0.00",CSTR(Fields!AUGTOT.Value) & "%", "REPLACED!")
try this:
=IIf(Fields!AUGNA.Value="0.00",CSTR(Fields!AUGTOT.Value) & "%" ,CSTR((Fields!AUGTOT.Value/Fields!AUGNA.Value)*100) & "%")

I usually use a custom divide function and call that function for division.
1. Right click on the Report area.
2. Select Report Properties
3. In the Properties Box, select Code and put the following code in the box.
Public Function Divide(ByVal numerator As Double, ByVal denominator As Double) As Double
If denominator = 0 Then
Return 0
Else
Return numerator / denominator
End If
End Function
You expression to call this function would look something like the one below:
=IIf(Fields!AUGNA.Value=0.00,CSTR(Fields!AUGTOT.Value) & "%",CSTR(Code.Divide(Fields!AUGTOT.Value,Fields!AUGNA.Value)*100) & "%")

Sorry for the late response, but if others get here with a similar problem...
It appears that the IIf statement evaluates both branches and then tests the expression for true / false. Thus, if you have a function or expression in one of the branches that throws an error (such as divide by zero), the whole thing returns #Error.
This also seems to be true for Switch statements
The only way I have found around this is to create a custom function in the Report Code section, send it the required parameters and let it do the comparison and return the correct value.

Related

How to evaluate an expression in Access like evaluating a formula in excel

Is there a way to evaluate an expression in Access like we can evaluate formulae in Excel? My Dlookup keeps resulting in the number two and I can't figure out where the hang up is.
=Nz(DLookUp("[RemanChangePoint]![ID]","[RemanChangePoint]","[NewPartNum] Like '*" & [LegacyPN1] & "*' Or [NewPartNum] Like '*" & [LegacyPN2] & "*'"),"")
There's 10 more LegacyPNs and I am expecting to get either the ID of the record that has the LegacyPN or a blank. Before, instead of doing it as above, I was doing:
Like & Chr(34) & "*" & [LegacyPN#] & "*" & Chr(34) & " Or..."
It would result in 2 every time. But now with the shortened version, it's resulting in blanks every time, even though there are records with the part number.
I have a similar expression on a different form, but only one LegacyPN to use as the lookup so it works. I assume that the problem is the numerous Or statements, but it doesn't make sense to me why it's not working, thus the desire for an expression evaluator like Excel has.
Or (and this may be a little uncouth to ask a slightly different question)
Is there a way to use an attachment's file name as the criteria for Dlookup? That may prove more effective than going by LegacyPN, I just can't figure it out via the expression builder.
Your first DLookup where clause is referring to fields on the current form since the double quotes cause the expression to break out of the where clause and into the scope of the ControlSource.
This means something like the following will be passed into the DLookup function because the [LegacyPN] portions of the expression are evaluated in the context of the form, before DLookup is called:
[NewPartNum] Like '*1*' Or [NewPartNum] Like '*2*'
...where 1 and 2 are values of [LegacyPN1] and [LegacyPN2] on the current form.
If you want the [LegacyPN] fields in the DLookup where clause to refer to the [RemanChangePoint] table being queried by the DLookup, then you need to fix your quotes:
=Nz(DLookUp("[RemanChangePoint]![ID]","[RemanChangePoint]", "[NewPartNum] Like '*' & [LegacyPN1] & '*' Or [NewPartNum] Like '*' & [LegacyPN2] & '*'"),"")
Your second syntax should have also worked because the Chr(34) added a double quote resulting in this final string being passed to the DLookup function in the where clause:
Like & "*" & [LegacyPN] & "*" & " Or..."
In which the [LegacyPN] field refers to the table within the DLookup query, not the current form. So I'm not sure why that didn't work. Note: single quotes or double quotes work okay in this context but not a mixture.
Double quotes get tricky within arguments of functions within a CountrolSource property (and other places).

#ERROR on =IIf(DCount [...] with Alphanumeric field

I am using a DCOUNT function to look at a table in access and see if a record exists - if it does then return a yes value.
=IIf(DCount("*","COMMENTS","[PROJECTID] = " & [PROJECTID])>0,"YES","")
This works great if my ProjectID is all numeric values - I only get #Error on the alphanumeric values.
I know that I have to make the ProjectID criteria a string but am having trouble placing the quotes.
First, break out the DCount piece and work on that by itself.
It sounds like your COMMENTS.PROJECTID field is text datatype, so add quotes before and after the value you concatenate into the third DCount argument (Criteria):
DCount("*", "COMMENTS", "[PROJECTID] = '" & [PROJECTID] & "'")
After you have that piece working correctly, add it into your IIf() function.

Textbox Formatting In SSRS 2008 - Break Line

So, say I have the following string
THIS IS STUFF (MORE STUFF)
How would I get the string to format as such in a textbox in SSRS?
THIS IS STUFF
(MORE STUFF)
This data is pulled from a single field in a query and I will not be able to manually inject a break line.
Also, sometimes the (More Stuff) is not in the field.
Extra examples:
STUFF AND THINGS
THINGS (STUFF)
THINGS AND STUFF (MORE STUFF)
You need to insert a line break into the string, i.e. set the textbox expression as something like:
="THIS IS STUFF" & vbcrlf & "(MORE STUFF)"
So the expression in a single textbox:
Will render as:
You can see there is a line break even though the string could fit on one line.
Edit after comment
OK, we need to handle various existing strings at the report level.
To do this, I would suggest using Custom Code, i.e. a function like:
Function SplitString (fieldValue As String) As String
If IsDBNull(fieldValue) OrElse IsNothing(fieldValue) Then
SplitString = ""
Else If InStr(fieldValue, "(") > 0
SplitString = RTrim(Left(fieldValue, InStr(fieldValue, "(") - 1)) & _
vbcrlf & Right(fieldValue, Len(fieldValue) - InStr(fieldValue, "(") + 1)
Else
SplitString = fieldValue
End If
End Function
I'm suggesting this as SSRS expression are notoriously brittle, and the lack of short circuiting means that what works for one string might display #Error on another. You could get around this with numerous IIf statements but Custom Code is much neater and easier to understand.
You can call the function with an expression like:
=Code.SplitString(Fields!MYFIELD.Value)
Either in a textbox expression or as a Calculated Field in the Dataset. The above works for me on your data:
I came across this issue while doing my project. And I would like to share my steps for this. Create individual placehloders within the textbox by selecting the expression and format each as you desired. You can easily add extra line by just moving the another item(another placeholder) to next line(by pressing enter-on the keyboard)
Here I created individual placeholders inside the list, gave the names and format just as I want.

If statement within VBscript function not working

I am hoping someone can help me possibly debug the below code, as I am confused as to why it is happening.
I have a fairly simple VBScript that runs when a user logs onto a server/PC, that will create some signatures based on their active directory details. I have decided to move each section of the signature creation into a function, in order to make things a bit easier when creating new signatures.
Here is the function I am having issues with:
'Function to add job title and company
Function AddTitle
objSelection.Font.Name = "Calibri"
objSelection.Font.Bold = False
objSelection.Font.Italic = False
objSelection.Font.Size = "11"
objSelection.Font.Color = RGB(0,0,0)
If(strTitle) Then
objSelection.TypeText strTitle & Chr(11)
End If
objSelection.TypeText strCompany & Chr (11)
End Function
Now, when calling the function later on using:
'Add job title and company
AddTitle
It ignores the section within the If statement. I know that some variables need to be defined globally for them to work in a function, and strTitle is definitely defined at the beginning of my script.
Am I missing something totally obvious, as the section inside the if statement functions correctly if taken out of the if statement. Likewise, If I were to add the if statement to my script inline, and take it out of the function, it works correctly.
It is only when running from the Function.
Apparently you have a global On Error Resume Next somewhere in your script, which suppresses the "Type Mismatch" error that the line If(strTitle) Then normally would raise.
strTitle probably contains a string value, so you can't use it like a boolean value in a conditional. Change the line to something like this:
If Trim(strTitle) <> "" Then
and your code should work as expected.

Textbox null problem

I have a textbox and a button on my Access form. In the click event of the button i want to see if the textbox is empty, if it is, nothing will be executed.
So i use
If Me.textbox.Value = Null Then
Exit Sub
End if
But it doesn't work... I checked the textbox.value in the execution window and it is Null, but the if clause just doesn't work... Why?
EDIT: #Dimse, I tried "", doesn't work. And also textbox.text = Null, it pops an error telling me the textbox is not active.. Very strange.
Null is never equal to anything, not even Null. Use the IsNull() function.
If IsNull(Me.textbox.Value) Then
If you want Me.textbox treated the same when it contains an empty string as when it's Null, concatenate an empty string to it and check the length of the combined string:
If Len(Me.textbox.Value & "") = 0 Then
You could also use the named constant, vbNullString, instead of the string literal, "", for an empty string.
If Len(Me.textbox.Value & vbNullString) = 0 Then
Using the string literal requires VBA to construct that string from scratch each time. With the named constant, VBA only needs to reference it, so should be faster and use less memory. However in many (probably most) cases, the performance advantage with vbNullString would be so minor that you wouldn't notice the difference. Also see the comment below from David-W-Fenton.
For me, the more compelling reason to use vbNullString is that it's instantly recognizable to my aging eyes. Conversely, with the string literal, it takes (a tiny bit) longer for me to confirm that "" is not actually something else ... like " " or "'". The only downside with vbNullString, IMO, is that requires more typing than "".
And finally, although you don't actually need to explicitly reference the Value property (since it's the default property of a text box), I left it in because you had it that way and because I prefer to be explicit with Value, too. :-)
I also apologize for being awaking the dead, but i'm wondering that no one has considered the use of Nz Function (see it #MSDN), very popular in VBA, also usable in Access/SQL and in my opinion the more convenient, concise and powerful solution for nullable values in expressions.
I apologize if I am awaking the dead, but just for completeness sake I am going to give the code for how to test for spaces (visibly 'blank/empty') as well:
If IsNull(Me.Textbox) Or Trim(Me.Textbox) = vbNullString Then
If Trim(Me.Textbox & vbNullString) = vbNullString Then 'Shorter version
If Len(Trim(Me.Textbox) & vbNullString) = 0 Then 'Shortest version
I came here looking for how to handle spaces, empty/ZLS, & NULL's
Null is not equal to another Null ;)
try If isNull(Me.textbox.Value) Then
Expand your sub like so:
If is null(Me.textbox.Value) Or (Me.textbox.Value = "") Then
Exit Sub
End if
I think you may need to check againt "", the empty string, and not Null.
I couldn't get this to work, since I was using the KeyUP event. So instead, this is what worked for me.
If(Textbox.Text = '')
...
Since Textbox.Value only updates on change event, it wasn't updating on keyup, so Textbox.Text is what is currently in the box.
Summary: Alternatively, Use the .Text property
Just use a second criteria, that will work !!
In this case just a simple word like "check".
If Forms![Basic]![Table.Item] & "check" = "check" Then
MsgBox "Field Empty"
Else
MsgBox "Field Not Empty"
End If
I saw this somewhere and thought I'd share:
If Len(Trim(Me.txtBox.Value)) > 0 Then