handling division by zero in access

miconian

Well-known Member
Joined
Aug 18, 2004
Messages
769
I have this field in query design:

PercentDif: [Difference]/[SumOfNet Amount]

Sometimes the denominator is zero, causing the result to be #error. How can I avoid this? Having it show up as zero would be fine.
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
There is probably an "elegant" way of doing this but I had written a function to do this for me a while back. Off the top of my head it was simply like this:

Code:
Function DivideByZero(numerator as long, denominator as long) as single
dim temp as single
if denominator = 0 then
  temp=0
else
  temp=numerator/denominator
end if
DivideByZero=temp
end Function

Then call it in your query

PercentDif: DivideByZero([Difference],[SumOfNet Amount])

Or something....

hth,

Rich
 
Upvote 0
The nice thing about a UDF is you can re-use it over and over without too much effort.

However, if you are looking for a straight calculation without the use of a UDF, it might look something like this:
Code:
PercentDif: IIF([SumOfNet Amount]<>0,[Difference]/[SumOfNet Amount],0)
 
Upvote 0

Forum statistics

Threads
1,216,175
Messages
6,129,312
Members
449,499
Latest member
HockeyBoi

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top