SSRS-Division Aggregation Inside IIF - reporting-services

I have been Written A Simple Expression To Display 0 When I divide by 0 As Exception:
I Use Visual Studio 2008 And Visual Studio 2019
=IIF ( Sum(Fields!STUDENTCOUNT.Value) = 0, 0 , Sum(Fields!CONTINUINGSTUDENTCOUNT.Value) /
Sum(Fields!STUDENTCOUNT.Value) )

You didn't explain the issue very well but I can see what the issue is, just from the expression. With IIF statements, the entire statement is evaluated as soon as the cell is reached in the execution. This means that no matter how you try to avoid the divide by zero error in this expression, it will still evaluate the false condition and generate the error. I'm going to borrow some custom code from this answer to give you the solution.
Public Function Divide (ByVal Dividend As Double, ByVal Divisor As Double)
If IsNothing(Divisor) Or Divisor = 0
Return 0
Else
Return Dividend/Divisor
End If
End Function
To add this to your code, right click outside of the report in the designer window and go to Report Properties. Click the Code tab and enter the previous code into the editor. Then, to call the code, you'll use the following expression.
=Code.Divide(Sum(Fields!CONTINUINGSTUDENTCOUNT.Value), Sum(Fields!STUDENTCOUNT.Value))

Related

IIF keyword is not working well Expression (RDLC Report)

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.

VBA Overflow Error 6 Not really

=Count([qty_rec])/Count([sub]) is the control source for a text box. Format for the control is 'percentage'. I have also tried 'standard', fixed and leaving it blank. I suspect the evaluation is comparing apples to oranges.
Private Sub Form_Current()
If Me.txt_percent_received = 0.5 Then
Me.cbtn_shortage.Visible = False
Else
End If
End Sub
I put breakpoint at If and goto to immediate and type ?Me.txt_percent_received
It says 0.5
Why does system think this number is too big?
and why does it work if I step thru the code?
Updated
Dim intTemp As Integer
intTemp = Me.txt_percent_received
If intTemp = 0.5 Then
Me.cbtn_shortage.Visible = False
Else
End If
Now the error is on intTemp = Me.txt_percent_received
qty_rec and sub are for totaling populated fields.
I have reproduced the effect, and I think what happens here is:
Calculated controls are evaluated after the form has finished loading. If you remove the event code, and open the form, you will notice that the value of txt_percent_received is displayed with a slight delay.
You may even notice that at first it displays #Number!, and then calculates the value.
But the form events happen before that, so what you get is a division of 0/0, and this gets runtime error 6 "Overflow".
1/0 would get error 11 "Division by zero".
You need a different way either to calculate txt_percent_received, or to implement your cbtn_shortage logic.

Conditional Formatting via VBA MS Access Report not working

I am having a trouble while trying to conditionally format the exhibition of records in a report inside a MS Access 2007 form.
I have search the Internet and I have seen lots os fellows stating that it is possible to perform visual changes in a single record via code implementing the method Detail_Paint() for the event Paint of the Detail section in a Report. Those people say that something like this is going to work:
Private Sub Detail_Paint()
val = CStr(Me.someTextBox.Value)
If val = "constraint" Then
Me.lineStrikethrough.BorderStyle = 0
End If
End Sub
The problem is that although the reading statement Me.someTextBox.Value returns the value of each record when the Paint event is thrown, the writing statement Me.lineStrikethrough.BorderStyle = 0 writes the value of the property BorderStyle for every single line in my report, not only for the one respecting the single record whose value I read from someTextBox field.
Can anyone tell me why such is happening? If this is the correct behaviour (although it does not seem right to me), how can I achieve my goal?
Note: lineStrikethrough is used to perform a strikethrough effect over the record in a Report. If there is another way to do that I would be happy to know.
Two things:
1 - Use the Detail's On Print event and not On Paint event.
Private Sub Detail_Print(Cancel As Integer, PrintCount As Integer)
val = CStr(Me.someTextBox.Value)
If val = "constraint" Then
Me.lineStrikethrough.BorderStyle = 0
End If
End Sub
2 - To see the conditional formatting, always open your report in Print Preview and not Report View.
You are really close!! :-) In my testing, it appears that the Detail_Paint() event allows you to change the formatting of controls, but changes will continue in subsequent records until you change/reset them to something else.
In your code sample, you just need to add another line to turn the strikethrough BorderStyle back on when the condition is no longer met.
Private Sub Detail_Paint()
Val = CStr(Me.someTextBox.Value)
If Val = "constraint" Then
Me.lineStrikethrough.BorderStyle = 0
Else
Me.lineStrikethrough.BorderStyle = 1
End If
End Sub
I found this clue on a code sample on MSDN which demonstrates conditional formatting using the Detail_Paint() method.
I found that Select Case to work best in the detail paint over if statements for a continuous form.
'Private Sub Detail_Paint()
Select Case CStr(Me.someTextBox.Value)
Case "constraint"
Me.lineStrikethrough.BorderStyle = 0
Case Else
Me.lineStrikethrough.BorderStyle = 1 'or what the default value is
End Select

VBA 2013 equivalent of IsNothing

I am currently debugging a DB application originally written in VBA 2005 that doesn't work with VBA 2010 and above versions. One of the (many) problems is that it uses the function IsNothing to test if an object variable has an object attached to it. This function seems to have been deprecated in 2010 and 2013. Is there an equivalent to this function in VBA 2013?
As others have indicated, IsNothing has never been a VBA function and was likely written as syntactic sugar for Is Nothing.
VB has an IsNothing function that serves the same purpose. In my experience, defining VBA functions that mirror familiar VB functions is common.
Here is a VBA IsNothing implementation from Tek-Tips that should be what you need - either to make the IsNothing calls work as-is...or provide a basis to inline them with equivalent code and strip the syntactic sugar:
'**********************************************************
'* Function: Returns true if argument evaluates to nothing
'* 1. IsNothing(Nothing) -> True
'* 2. IsNothing(NonObjectVariableOrLiteral) -> False
'* 3. IsNothing(ObjectVariable) -> True if instantiated,
'* otherwise False
'**********************************************************
Public Function IsNothing(pvarToTest As Variant) As Boolean
On Error Resume Next
IsNothing = (pvarToTest Is Nothing)
Err.Clear
On Error GoTo 0
End Function 'IsNothing
In fairness, note that this is essentially what #HansUp suggested already, just sourced elsewhere - and prefaced by a best-guess how the code you're debugging likely got the way it is.
Access VBA does not include an IsNothing function. If you had one in the past, it was a custom function.
If you can't track down the old version, you could create a new one.
Public Function IsNothing(ByRef pObject As Variant) As Boolean
IsNothing = (pObject Is Nothing)
End Function
How about
Dim x as Object
If x is Nothing then
IsNothing itself has never been a VBA function...
If obj Is Nothing Then...
may work for you, but may not yield the same results as the IsNothing function you are expecting. You may need to code more specific checks depending on what you want to determine.

Access Report won't allow me to refer to a field in VBA unless it's on the report in its own right

So, in an Access Form or Report a good way to show something dynamic on the screen that is more complex than =[UnitPrice]*[Quantity] is to drop into VBA.
e.g. in this simplified example, the underlying table for this Report has two fields ShowTax and TaxRate. By making a TextBox's control source =GetTaxInfo I get to introduce some complexity in VBA:
Public Function GetTaxInfo() As String
Dim result As String
If Me!ShowTax = 0 Then
result = "Tax included # " & Me!TaxRate
Else
result = ""
End If
GetTaxInfo = result
End Function
OK, this works ... so long as I have a field somewhere else that refers to TaxRate. Otherwise it just prints #Error. It's as if it needs to preload the field before it can be used in VBA code. It isn't the end of the world because I can have a bunch of fields in the report all set to not be visible, but it's untidy.
So is it the case that you can't refer to a field in VBA code backing a report unless you have already referred to the field in the conventional way as a field baked into the report?
I can't remember encountering this limitation before. Or is it that I have a corrupt report? (I have tried the usual compact/repair, export/reimport the Report etc)
Edit:
the weird thing is ... now it's working again. And - I'm pretty sure - there is no control in the report. which is why I was thinking it was a corruption in the report.
You'll need a control on the form/report.
If this is too messy, you could put the function in a Module and use in the RecordSource (based on a query). No sense burying all this logic in a report when it could be used in other places as well.
Public Function GetTaxInfo(ShowTax as Boolean, TaxRate as Single) As String
Dim result As String
If ShowTax = 0 Then
result = "Tax included # " & TaxRate
Else
result = ""
End If
GetTaxInfo = result
End Function
Then the control is set to this field in this report and others.