简体   繁体   English

MS Access 2013-通过零错误获得毫无根据的划分

[英]MS Access 2013 - Getting unfounded Division by zero error

I have a routine that writes totals to textboxes to prevent expensive 'Dlookup' processing time. 我有一个例程将总计写入文本框,以防止使用昂贵的“ Dlookup”处理时间。

Some of the totals produce a percentage rate so depend on a denominator. 一些总数产生一个百分比,因此取决于分母。

The error seems to be due to lag of some sort because the denominators are never blank or zero. 该错误似乎是由于某种滞后引起的,因为分母永远不会为空或为零。 For example, as long as there are values in this database - which there are always an abundance of, these denominators will never be zero. 例如,只要此数据库中存在值-总是有很多值,这些分母就永远不会为零。

At any rate, here is the line that gets the error ( I get an error 11) 无论如何,这是发生错误的行(我得到错误11)

Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim rsSP As DAO.Recordset

Set db = CurrentDb
--->Set rs = db.OpenRecordset("TOTALS_FINAL", dbOpenSnapshot)

What can I do to trap this error? 我该怎么办才能捕获此错误? It only happens occasionally and when I tell the use to restart the database (which frees up some resources) it usually corrects the problem. 它仅偶尔发生,当我告诉用户重新启动数据库(这会释放一些资源)时,通常可以解决此问题。

Here is the SQL for TOTALS final 这是TOTALS最终版本的SQL

SELECT
    "Total Program" AS BL
   ,TOTALS_COB_ALL_TOTALS.AFP_ AS AFP
   ,TOTALS_COB_ALL_TOTALS.ALLT_ AS ALLT
   ,TOTALS_COB_ALL_TOTALS.SP_C_ AS SP_C
   ,TOTALS_COB_ALL_TOTALS.SP_O_ AS SP_O
   ,TOTALS_COB_ALL_TOTALS.COMMITS_ AS COMMITS
   ,TOTALS_COB_ALL_TOTALS.OBS_ AS OBS
   ,TOTALS_COB_ALL_TOTALS.RA_ AS RA
   ,IIF([SP_C_] = 0, 0, [COMMITS_] / [SP_C_]) AS COM_SP_RATE
   ,IIF([SP_O_] = 0, 0, [OBS_] / [SP_O_]) AS OBS_SP_RATE
   ,IIF(Nz([AFP_], 0) = 0, 0, Nz([OBS_], 0) / [AFP_]) AS OB_AFP_RATE
   ,TOTALS_COB_ALL_TOTALS.UNC_ AS UNC
   ,TOTALS_COB_ALL_TOTALS.AVL_ AS AVL
   ,TOTALS_COB_ALL_TOTALS.ACW_ AS ACW
   ,7999 AS SO
FROM
    TOTALS_COB_ALL_TOTALS;

The query right before it just collects totals that are used to produce the final percentages 在查询之前,它只是收集用于产生最终百分比的总计

The reason is, that IIf always evaluates both expressions even though only one is used. 原因是,即使只使用一个表达式, IIf总是对两个表达式求值。

So use a "double-iif" that divides by one and not zero: 因此,请使用除以一而不是零的“ double-iif”:

,IIF([SP_C_] = 0, 0, [COMMITS_] / IIf([SP_C_] = 0, 1, [SP_C_])) AS COM_SP_RATE
,IIF([SP_O_] = 0, 0, [OBS_] / IIf([SP_O_] = 0, 1, [SP_O_])) AS OBS_SP_RATE
,IIF(Nz([AFP_], 0) = 0, 0, [OBS_] / IIf(Nz([AFP_], 0) = 0, 1, [AFP_]) AS OB_AFP_RATE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM