除以0当没有发生除法时出错

时间:2015-06-03 22:27:33

标签: sql vba ms-access

我在vba中运行一个简单的插入查询

DoCmd.RunSQL "INSERT INTO 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 FROM qryBldgComparePer;", False

然而,当我运行此查询时,我得到除以0错误。它所基于的查询,qryBldgComparePer显示正常,并且没有除以0错误。它有可能存在的可能性,但是如果分母为0则可以将其放入IIF语句的字段中,而不是使字段= 0。这是插入时以某种方式被绕过,所以它试图除以0?我已经在线查看,并没有找到这样的另一个案例。你们提供的任何帮助都将非常感激。谢谢!

这是导致错误的所有代码:

strSQL = "SELECT bc.Bldg, bc.Utility, bc.LYRAVGUnits, IIF( bc.LYRAvgUnits = 0, NULL, Abs(([bc].[LYRAvgUnits]-[bc].[CurUnits])/[bc].[LYRAvgUnits])) AS [LYRAVGUNITDif], bc.LYRUnits, IIF( bc.LYRUnits = 0, NULL, Abs(([bc].[LYRUnits]-[bc].[CurUnits])/[bc].[LYRUnits])) AS [LYRUNITDif], bc.PrevMonUnits, IIF( bc.PrevMonUnits = 0, NULL, Abs(([bc].[PrevMonUnits]-[bc].[CurUnits])/[bc].[PrevMonUnits])) AS [PrevMonUNITDif], bc.CurUnits, bc.LYRAVGTotCost, IIF(bc.LYRAvgTotCost = 0, NULL, Abs(([bc].[LYRAvgTotCost]-[bc].[CurTotCost])/[bc].[LYRAvgTotCost])) AS [LYRAVGTotCostDif], bc.LYRTotCost, IIF(bc.LYRTotCost = 0, NULL, Abs(([bc].[LYRTotCost]-[bc].[CurTotCost])/[bc].[LYRTotCost])) AS [LYRTotCostDif], bc.PrevMonTotCost, IIF( bc.PrevMonTotCost = 0, NULL, Abs(([bc].[PrevMonTotCost]-[bc].[CurTotCost])/[bc].[PrevMonTotCost])) AS [PrevMonTotCostDif], bc.CurTotCost FROM qryBldgCompare AS bc " & _
"WHERE (bc.CurUnits <> 0) AND ((((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 FROM tblBldgComparePer;"
DoCmd.RunSQL "INSERT INTO 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 FROM qryBldgComparePer;", False

1 个答案:

答案 0 :(得分:2)

您的WHERE条款中可能会发生这种情况:

WHERE (bc.CurUnits <> 0) AND 
      ((((Abs(([bc].[LYRAvgUnits]-[bc].[CurUnits])/[bc].[LYRAvgUnits]))>" & PerDif & "))

AND在SQL中没有短路,因此即使bc.CurUnits0,右侧也会得到评估。

我相信您也可以将其更改为IIF

WHERE (IIF(bc.CurUnits = 0 , 0, Abs(([bc].[LYRAvgUnits]-[bc].[CurUnits])/[bc].[LYRAvgUnits])>" & PerDif & ")