Link to home
Start Free TrialLog in
Avatar of SteveL13
SteveL13Flag for United States of America

asked on

Division by zero error

Why am I getting a Division by zero error with this code:

Me.txtACV = Me.txtTotalEstimatedReplacementCost * (Me.txtUsefulLife - Me.txtCurrentLifeOfAsset) / Me.txtUsefulLife
Avatar of Joe Howard
Joe Howard
Flag of United States of America image

What are the values of the textboxes when running the code. I believe the error message tells you exactly what the problem is, your dividing a number by 0.
try this


Me.txtACV = IIF(nz(Me.txtUsefulLife,0)=0,0,Me.txtTotalEstimatedReplacementCost * (Me.txtUsefulLife - Me.txtCurrentLifeOfAsset) / Me.txtUsefulLife)
Avatar of SteveL13

ASKER

Didn't work.  Still get div by zero error
ASKER CERTIFIED SOLUTION
Avatar of Gustav Brock
Gustav Brock
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Dale is correct but interestingly, the IIf() works differently in VBA than it does in SQL.  In VBA, all clauses of the IIf() are evaluated and so you get the divide by zero error whether it is in the true path or the false path but if you used the same expression in a query, only the true path is evaluated so you would not get the divide by zero error.  Because of this behavior, I almost never use the IIf() in code.