Results 1 to 4 of 4
  1. #1
    George is offline Competent Performer
    Windows 7 64bit Access 2013 64bit
    Join Date
    Feb 2012
    Posts
    295

    Summing the values in a table column

    Good Day All,

    I want to sum the values in a table but can't think of how to achieve this objective.

    Here Is my attempt which obviously doesn't work.



    Code:
    Private Sub CustomerPayments_Click()
     Dim curDatabase As Database
        Dim rst As Recordset
         
                        
        Set curDatabase = CurrentDb
        
        Set rst1 = curDatabase.OpenRecordset("Account General Journal")
        
       If Not sum(rst1.Fields("Debit")) = sum(rst1.Fields("Credit")) Then
       
          MsgBox "unequal"
        
       End If
      
      Set rst = Nothing
      Set curDatabase = Nothing
      
    End Sub
    Can anyone give guidance on this problem?

    Also, since I am not editing the table was it necessary to open it?

  2. #2
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,389
    You don't need to open a recordset. Go to the table directly...

    Code:
    If dsum("Debit","[Account General Journal]") <> dsum("Credit","[Account General Journal]") then
    Last edited by davegri; 09-02-2017 at 08:30 AM. Reason: syntax

  3. #3
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142
    (EDIT: Yeah, what Dave said! )

    Maybe try this (untested):
    Code:
    Private Sub CustomerPayments_Click()
    On Error GoTo Error_Proc
        Dim curDatabase As Database
        Dim rst1 As Recordset
                           
        Set curDatabase = CurrentDb
        
        Set rst1 = curDatabase.OpenRecordset("SELECT Sum([Debit]) AS SumDebit, Sum([Credit]) AS SumCredit FROM [Account General Journal]")
        
        If Not rst1.EOF Then
            If rst1!SumDebit <> rst1!SumCredit Then
       
                MsgBox "unequal"
                
            ElseIf IsNull(rst1!SumDebit) Then
            
                MsgBox "No records in [Account General Journal] to sum."
                
            End If
        End If
      
        rst1.Close
        curDatabase.Close
      
    Exit_Proc:
        Set rst1 = Nothing
        Set curDatabase = Nothing
        Exit Sub
        
    Error_Proc:
        MsgBox "There was an error"
        Resume Exit_Proc
    End Sub

  4. #4
    George is offline Competent Performer
    Windows 7 64bit Access 2013 64bit
    Join Date
    Feb 2012
    Posts
    295
    Thanks. They are all working fine.

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

Similar Threads

  1. Replies: 14
    Last Post: 07-13-2015, 12:47 PM
  2. Replies: 5
    Last Post: 04-07-2015, 02:20 PM
  3. Summing a query column help
    By Eskoraczewski in forum Access
    Replies: 1
    Last Post: 02-01-2014, 04:27 AM
  4. Replies: 1
    Last Post: 02-27-2012, 09:50 AM
  5. Summing a column
    By nashr1928 in forum Forms
    Replies: 2
    Last Post: 05-10-2010, 05:42 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