How to avoid 'Divide by zero' error in report?

I’m designing a report in which I calculate a Margin % in the Body: IF Amount <> 0 THEN gDecMargin := (Amount - gDecCostPrice) / (Amount/100) Then I want to have this summarized in the GroupFooter. This works perfectly with totalfields like this: CurrReport.CREATETOTALS(gDecCostPrice). Only, this works fine for amounts en cost prices, but not for a calculated margin %, as this results in an addition of al detail percentages! If I calculate the % in the GroupFooter I get a ‘divide by zero’ error, so this isn’t an option either. Is there a way to calculate this % in a new field in the GroupFooter like a do in the Body, but then directly in the fields SourceExpression and without having the ‘divide by’ error?

Hi Michael, of course it’s no use to create a total for the percentage value… what you need to do is either put your formula in the source expression of the field you want to use, but make sure that you also create the total for the amount variable! The other option is, which is more elegant to avoid the “divide by zero” error, is to add the code line you mention, in the code of the footer section. Saludos Nils

The “cleanest” solution I find, is to add a function to the source table of the report, to return the Percent value. THen you just use the function as the source expression in the report:

MyDivFunction(Var1,Var2) : Decimal;
if Var1 = 0 then
  exit(0);
exit((Var1 - Var2) / (Var1 / 100));

then in the report just add the source expression:

mydivfunction(Amount,CostPrice)

_________________________ David Singleton Navision Consultant since 1991 dmks22@comcast.net___________

quote:


Originally posted by nilsm: The other option is, which is more elegant to avoid the “divide by zero” error, is to add the code line you mention, in the code of the footer section.


Thanks Nils. Works fine! Adios, Michiel Groeten, Michiel Hageman ICT & quality manager PM Komponenten B.V. The Netherlands