简体   繁体   中英

MS Access 2013 - Getting unfounded Division by zero error

I have a routine that writes totals to textboxes to prevent expensive 'Dlookup' processing time.

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)

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

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.

So use a "double-iif" that divides by one and not zero:

,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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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