sql - Division by 0 Error when no division is happening -
i running simple insert query in vba
docmd.runsql "insert tblbldgcompareper ( bldg, utility, lyravgunits, lyravgunitdif, lyrunits, lyrunitdif, prevmonunits, pmunitdif, currentunits, avgoftotcost, lyravgcostdif, lyrtotcost, lyrcostdif, prevmontotcost, pmcostdif, currenttotcost ) select qrybldgcompareper.bldg, qrybldgcompareper.utility, qrybldgcompareper.lyravgunits, qrybldgcompareper.lyravgunitdif, qrybldgcompareper.lyrunits, qrybldgcompareper.lyrunitdif, qrybldgcompareper.prevmonunits, qrybldgcompareper.prevmonunitdif, qrybldgcompareper.curunits, qrybldgcompareper.lyravgtotcost, qrybldgcompareper.lyravgtotcostdif, qrybldgcompareper.lyrtotcost, qrybldgcompareper.lyrtotcostdif, qrybldgcompareper.prevmontotcost, qrybldgcompareper.prevmontotcostdif, qrybldgcompareper.curtotcost qrybldgcompareper;", false however getting divide 0 error when run query. query based on, qrybldgcompareper shows fine, , has no divide 0 errors in it. there possibilites could, fields put in iif statement if denominator 0 make field = 0 instead. somehow being bypassed when inserted, trying divide 0? have looked online , have not found case this. guys can offer appreciated. thank you!
this code leading error:
strsql = "select bc.bldg, bc.utility, bc.lyravgunits, iif( bc.lyravgunits = 0, null, abs(([bc].[lyravgunits]-[bc].[curunits])/[bc].[lyravgunits])) [lyravgunitdif], bc.lyrunits, iif( bc.lyrunits = 0, null, abs(([bc].[lyrunits]-[bc].[curunits])/[bc].[lyrunits])) [lyrunitdif], bc.prevmonunits, iif( bc.prevmonunits = 0, null, abs(([bc].[prevmonunits]-[bc].[curunits])/[bc].[prevmonunits])) [prevmonunitdif], bc.curunits, bc.lyravgtotcost, iif(bc.lyravgtotcost = 0, null, abs(([bc].[lyravgtotcost]-[bc].[curtotcost])/[bc].[lyravgtotcost])) [lyravgtotcostdif], bc.lyrtotcost, iif(bc.lyrtotcost = 0, null, abs(([bc].[lyrtotcost]-[bc].[curtotcost])/[bc].[lyrtotcost])) [lyrtotcostdif], bc.prevmontotcost, iif( bc.prevmontotcost = 0, null, abs(([bc].[prevmontotcost]-[bc].[curtotcost])/[bc].[prevmontotcost])) [prevmontotcostdif], bc.curtotcost qrybldgcompare bc " & _ "where (bc.curunits <> 0) , ((((abs(([bc].[lyravgunits]-[bc].[curunits])/[bc].[lyravgunits]))>" & perdif & ")) or (((abs(([bc].[lyrunits]-[bc].[curunits])/[bc].[lyrunits]))>" & perdif & ")) or (((abs(([bc].[prevmonunits]-[bc].[curunits])/[bc].[prevmonunits]))>" & perdif & ")) or (((abs(([bc].[lyravgtotcost]-[bc].[curtotcost])/[bc].[lyravgtotcost]))>" & perdif & ")) or (((abs(([bc].[lyrtotcost]-[bc].[curtotcost])/[bc].[lyrtotcost]))>" & perdif & ")) or (((abs(([bc].[prevmontotcost]-[bc].[curtotcost])/[bc].[prevmontotcost]))>" & perdif & ")));" currentdb.querydefs("qrybldgcompareper").sql = strsql docmd.runsql "delete tblbldgcompareper;" docmd.runsql "insert tblbldgcompareper ( bldg, utility, lyravgunits, lyravgunitdif, lyrunits, lyrunitdif, prevmonunits, pmunitdif, currentunits, avgoftotcost, lyravgcostdif, lyrtotcost, lyrcostdif, prevmontotcost, pmcostdif, currenttotcost ) select qrybldgcompareper.bldg, qrybldgcompareper.utility, qrybldgcompareper.lyravgunits, qrybldgcompareper.lyravgunitdif, qrybldgcompareper.lyrunits, qrybldgcompareper.lyrunitdif, qrybldgcompareper.prevmonunits, qrybldgcompareper.prevmonunitdif, qrybldgcompareper.curunits, qrybldgcompareper.lyravgtotcost, qrybldgcompareper.lyravgtotcostdif, qrybldgcompareper.lyrtotcost, qrybldgcompareper.lyrtotcostdif, qrybldgcompareper.prevmontotcost, qrybldgcompareper.prevmontotcostdif, qrybldgcompareper.curtotcost qrybldgcompareper;", false
it's happening in where clause:
where (bc.curunits <> 0) , ((((abs(([bc].[lyravgunits]-[bc].[curunits])/[bc].[lyravgunits]))>" & perdif & ")) and not short-circuiting in sql right-hand side getting evaluated if bc.curunits 0.
i believe can change iif well:
where (iif(bc.curunits = 0 , 0, abs(([bc].[lyravgunits]-[bc].[curunits])/[bc].[lyravgunits])>" & perdif & ")
Comments
Post a Comment