Results 1 to 5 of 5
  1. #1
    lawdy is offline Competent Performer
    Windows 7 32bit Access 2013
    Join Date
    Jan 2013
    Posts
    216

    Trying to sum a query

    I want to sum the results of the query listed below.

    Public Function modQue_dayTotal2()
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    Dim sql As String
    Dim strSvc As String
    Dim dateDate As Date
    Dim dblAmt As Double

    strSvc = "wash"
    pubDateDue = DateValue("9/3/2025")
    Set db = CurrentDb
    sql = "SELECT * FROM tblCalendar WHERE fDate = #" & pubDateDue & "# " _
    & "AND fTxtSvc = '" & strSvc & "'"
    Set rs = db.OpenRecordset(sql)

    Do While Not rs.EOF
    dblAmt = rs!fDblRevenue
    rs.MoveNext
    dblAmt = dblAmt + dblAmt
    Loop
    Debug.Print "dblAmt " & dblAmt


    rs.Close
    Set rs = Nothing
    Set db = Nothing

    End Function


    This works, but it looks like DSum would be a much better way of doing it. I have not been able to figure out the code for that. Any ideas?

  2. #2
    xps35's Avatar
    xps35 is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Jun 2022
    Location
    Schiedam, NL
    Posts
    299
    Try
    Code:
    DSum("fDblRevenue", "fDblRevenue", "fTxtSvc = 'wash' AND fDate = #09-03-2025#")
    Groeten,

    Peter

  3. #3
    lawdy is offline Competent Performer
    Windows 7 32bit Access 2013
    Join Date
    Jan 2013
    Posts
    216
    Thanks xps35. That works and I don't need the query at all. Thanks again.

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,550
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  5. #5
    Join Date
    Jun 2010
    Location
    Belgium
    Posts
    1,180
    if you want to total a field the agonizing RBAR solution of looping through records is the slowest and most resource consuming solution.
    Better is the DSum option
    The fastest solution is the totals query option:

    Code:
    select sum(fDblRevenue) from tblCalendar WHERE fDate = #" & pubDateDue & "# " _
    & "AND fTxtSvc = """" & strSvc & """"
    
    with an index on the criteria fields. Of course, check the update/index usage ratio before creating the index.

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

Similar Threads

  1. Data Type Mismatch when Trying to Sum a Qry
    By Tuckejam in forum Queries
    Replies: 6
    Last Post: 07-07-2020, 10:58 AM
  2. Replies: 1
    Last Post: 10-21-2015, 05:16 PM
  3. Sum of a Sum on a query
    By keiath in forum Forms
    Replies: 2
    Last Post: 02-17-2014, 07:25 PM
  4. Replies: 1
    Last Post: 02-01-2013, 11:23 PM
  5. Trying to sum a group of records not working
    By shelbsassy in forum Reports
    Replies: 0
    Last Post: 04-10-2011, 07:52 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