Results 1 to 9 of 9
  1. #1
    Gedeon is offline Novice
    Windows 8 Access 2013
    Join Date
    Oct 2015
    Posts
    3

    Query that calculates from the previous values.

    Dear friends. I'm trying to create a query like in the picture.
    Click image for larger version. 

Name:	Capture.JPG 
Views:	17 
Size:	29.0 KB 
ID:	22605
    value_1 is separately calculated.
    value_c is calculated based on value_1.
    My problem is to calculate 'value_total' which should get the last calculated data and add current data.
    I have tried "DSum" and "Dlast" functions but this hangs or calculates very slow.


    Does anyone know how to solve this problem?

  2. #2
    Gedeon is offline Novice
    Windows 8 Access 2013
    Join Date
    Oct 2015
    Posts
    3
    any ideas ?

  3. #3
    NoellaG's Avatar
    NoellaG is offline VIP
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Jun 2010
    Location
    Belgium
    Posts
    1,180
    Hi,

    running totals in a query are hard to do, but you could do the first calculation in a query and then try the running total in a report.
    If you need to have it in a query, then you could use an intermediate table and use VBA to populate it with the running totals.

    succes
    NG

  4. #4
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 8 Access 2013
    Join Date
    Aug 2013
    Posts
    7,862
    The only other way I know of is to take a trip to the data using Dlookup. It should be a little faster than Dmax or Dsum because you can use criteria with Dlookup.

    DLookUp("[MyField]","MyTable","[ID]=" & [ID]-1)
    https://support.microsoft.com/en-us/kb/210504

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    What was the expression you tried with DSum?

    DMax and DSum can use filter criteria as well. All of the domain aggregate functions have WHERE CONDITION argument.

    DSum("ValueField", "MyTable", "DateField<=#" & [datefield] & "#")

    DLookup won't return a running sum, it only returns single value of record specified in the WHERE CONDITION.

    Yes, domain aggregate can perform very slowly in queries. As NoellaG suggests, a report is the best vehicle for this sort of calculation because then textbox has RunningSum property.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  6. #6
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 8 Access 2013
    Join Date
    Aug 2013
    Posts
    7,862
    Quote Originally Posted by June7 View Post
    ...DLookup won't return a running sum, it only returns single value of record specified in the WHERE CONDITION....
    Correct, I am imagining avoiding Dsum and incorporating the Dlookup in a calc. So something like
    MyValueTotal: DLookUp("[MyField]","MyTable","[ID]=" & [ID]-1)+[MyField]

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    Still not seeing how that produces a running sum - it just adds two adjacent records which can be done with a nested subquery. http://allenbrowne.com/subquery-01.html#AnotherRecord

    And it assumes there are no gaps in the ID sequence.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  8. #8
    Gedeon is offline Novice
    Windows 8 Access 2013
    Join Date
    Oct 2015
    Posts
    3
    Thank you very much for your answers. I'll try all the aforementioned methods and will write a feedback soon.

  9. #9
    InsuranceGuy is offline Competent Performer
    Windows 7 64bit Access 2013
    Join Date
    Aug 2015
    Location
    Colorado
    Posts
    126
    Code:
    RunningSum: (select sum([DayCount]) from SampleTable ST where ST.dateField<=SampleTable.dateField)
    Quote Originally Posted by June7 View Post
    What was the expression you tried with DSum?

    DSum("ValueField", "MyTable", "DateField<=#" & [datefield] & "#")
    I completely agree with June.

    DLookup is not suited to your need for a running total because it will only look up a single field result from a source, not from within the query dataset you are building--so you can't build a recursive consideration within the same query using DLookup. You can do so with DSum and an appropriate criteria (A.K.A. a where filter)

    I would only add that you can do multiple criteria in all of the Domain functions, including DSum and DLookup.

    The issue with Domain functions is that they are VBA-based rather than JET-based. Simply put, JET-based functions are faster because they don't have to go consult the VBA libraries. Instead they run entirely in the database engine.

    If we take June's code that is a field definition:
    Code:
    DSum("ValueField", "MyTable", "DateField<=#" & [datefield] & "#")
    If we want it to run in JET (fast), we need to make some adjustments. The field definition should instead be:

    Code:
    RunningSum: (select sum([ValueField]) from MyTable MT where MT.dateField<=MyTable.dateField)
    You will also need to ensure you group by your MyTable.[dateField].

    Allen Browne covers this a bit here: http://allenbrowne.com/QueryPerfIssue.html

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

Similar Threads

  1. Replies: 43
    Last Post: 03-10-2015, 09:35 PM
  2. Replies: 7
    Last Post: 05-01-2013, 06:20 PM
  3. Replies: 6
    Last Post: 10-21-2011, 10:24 PM
  4. Replies: 0
    Last Post: 07-13-2011, 08:32 AM
  5. new database that calculates workhours per empolyee??
    By manos39 in forum Database Design
    Replies: 0
    Last Post: 02-16-2011, 03:36 AM

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