Results 1 to 6 of 6
  1. #1
    skydivetom is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Feb 2019
    Posts
    1,038

    Dcount results in error "Overflow"

    Experts:

    I'm running into an "overflow" error when using DCOUNT.

    Quick background:
    1. I'm executing 5 append queries as part of a routine.
    2. Prior to the execution of the 5 append queries, I drop all records from the 5 associated tables.
    3. Then, as part of the append, I set **DoCmd.SetWarnings False** in order to prevent the display of both a) # of records dropped and b) # of records appended for EACH of the 5 tables.

    Now, once the last append query has been executed (w/o any indication of # of records dropped and added), I want to throw a customized message box (using DCOUNT) to display the total # of records processed.

    For testing purposes, I'm using the code below:



    Code:
    Private Sub cmdDcount_Click()
    
        Dim RecordCount_MMAC As Integer
        Dim RecordCount_NNSY As Integer
        Dim RecordCount_PHNSY As Integer
        Dim RecordCount_PNSY As Integer
        Dim RecordCount_POAIRS As Integer
        Dim RecordCount_TOTAL As Integer
        
        RecordCount_MMAC = DCount("[AUTO_ID]", "tbl_MMAC")
        RecordCount_NNSY = DCount("[AUTO_ID]", "tbl_NNSY")
        RecordCount_PHNSY = DCount("[AUTO_ID]", "tbl_PHNSY")
        RecordCount_PNSY = DCount("[AUTO_ID]", "tbl_PNSY")
        RecordCount_POAIRS = DCount("[AUTO_ID]", "tbl_POAIRS")
        
        RecordCount_TOTAL = (RecordCount_MMAC + RecordCount_NNSY + RecordCount_PHNSY + RecordCount_PNSY + RecordCount_POAIRS)
        
        MsgBox "# of records: " & RecordCount_TOTAL
    
    End Sub
    When putting a breakpoint at **RecordCount_TOTAL = (RecordCount_MMAC...**, and then hover over the 5 record count lines, the correct # of records are displayed in the tooltip.

    However, once I remove the breakpoint and then run the function, an "Overflow error" (see attached JPG) is thrown.

    My question(s):
    1. How can I prevent the over flow error so that the message box: "MsgBox "# of records: " & RecordCount_TOTAL" shows me the sum of the 5 DCOUNTs?
    2. Is there a smoother way (e.g., using a ForLoop or DoWhile) vs. hard-coding the tables names? That is, all 5 tables for which I need to get the DCOUNT have the same table prefix = "tbl_"

    Thanks,
    Tom

    P.S. Please see update in post #2.
    Attached Thumbnails Attached Thumbnails OverflowError.JPG   RecordCount.JPG  

  2. #2
    skydivetom is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Feb 2019
    Posts
    1,038
    Quick update... changed declaration from "Integer" to "Long"....

    Msgbox now outputs the correct values.

    Question remains whether or not a ForLoop OR Dowhile can be applied to count total # of records for all tables starting with "tbl_"?

  3. #3
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    Sure, loop the TableDefs collection, checking for the first 4 characters being "tbl_". Accumulate the values in a variable within the loop.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  4. #4
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,716
    Here's a sample with For Loop
    Code:
    ' ----------------------------------------------------------------
    ' Procedure Name: recCountsAllTables
    ' Purpose: get a count of all records in a database
    ' Procedure Kind: Sub
    ' Procedure Access: Public
    ' Author: Jack
    ' Date: 14-Jul-21
    ' ----------------------------------------------------------------
    Sub recCountsAllTables()
        Dim tdf As DAO.TableDef
        Dim TotRecords As Long
        For Each tdf In CurrentDb.TableDefs
            Debug.Print tdf.name & "   " & tdf.RecordCount
            TotRecords = TotRecords + tdf.RecordCount
        Next tdf
        Debug.Print "Total Records in " & CurrentDb.name & "(" & TotRecords & ")"
    End Sub
    In my sample database
    .......
    ZmT1 5
    ZMT2 4
    ztemp_MilestoneFormat 3
    ZZZArticles 3
    Total Records in C:\Users\Jack\Documents\Database1_o365.accdb(58305 0)


    You can modify to select only certain tables as needed.

  5. #5
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,001
    If your table names aren't going to change, you have already done the hard work.
    You could get the tables names in a loop and do a dcount and simply add it up as you went through, but it seems like hard work to me, unless the table names will change frequently.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  6. #6
    skydivetom is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Feb 2019
    Posts
    1,038
    Orange -- your solution is perfect! I tweaked the code to only include records where table name starts with "tbl_".

    Cheers,
    Code:
    Private Sub cmdMethod3_Click()
    
        Dim tdf As DAO.TableDef
        Dim TotRecords As Long
        
        For Each tdf In CurrentDb.TableDefs
            If Left(tdf.Name, 4) = "tbl_" Then
                TotRecords = TotRecords + tdf.RecordCount
            End If
        Next tdf
    
        MsgBox "Total Records: " & TotRecords
    
    End Sub

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Combo box results in error "2448"
    By skydivetom in forum Forms
    Replies: 8
    Last Post: 07-21-2019, 03:14 PM
  2. Replies: 3
    Last Post: 07-30-2018, 05:45 PM
  3. Replies: 20
    Last Post: 07-29-2015, 12:41 PM
  4. Replies: 1
    Last Post: 11-13-2014, 02:54 PM
  5. "Overflow" error message.
    By joeferraro2000 in forum Queries
    Replies: 0
    Last Post: 03-08-2007, 06:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums