SteveL13
asked on
Division by zero error
Why am I getting a Division by zero error with this code:
Me.txtACV = Me.txtTotalEstimatedReplac ementCost * (Me.txtUsefulLife - Me.txtCurrentLifeOfAsset) / Me.txtUsefulLife
Me.txtACV = Me.txtTotalEstimatedReplac
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.tx tTotalEsti matedRepla cementCost * (Me.txtUsefulLife - Me.txtCurrentLifeOfAsset) / Me.txtUsefulLife)
Me.txtACV = IIF(nz(Me.txtUsefulLife,0)
ASKER
Didn't work. Still get div by zero error
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.