Share via

running sum in decending order

Anonymous
2018-10-06T13:00:53+00:00

I am relatively new to access programming and do not know a lot about VBA so I am search for a solution that can be found using queries.  I am essentially doing project management using access.  I have a list of ~ 30 tasks that are repeating for each job. The project cycle ranges from 2 to 3 years.  I list all the tasks in the data sheet view sorted by job number.  What I am struggling with is how to have a running total of the remaining time in descending order.   Below is an example of what I am looking to accomplish which I can do in excel.  Thanks in advance for your assistance.

Job # task duration start finish time remaining
A task 1 2 97
A task 2 3 95
A task 3 3 92
A task 4 4 89
A task 5 5 85
A task 6 10 80
A task 7 10 70
A task 8 50 60
A task 9 5 10
A task 10 5 5
B task 1 2 97
B task 2 3 95
B task 3 3 92
B task 4 4 89
B task 5 5 85
B task 6 10 80
B task 7 10 70
B task 8 50 60
B task 9 5 10
B task 10 5 5
Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

5 answers

Sort by: Most helpful
  1. Anonymous
    2018-10-09T21:25:35+00:00

    At present the SQL statement includes the expression VAL(MID(Task,INSTR(Task," ")+1)) which parses the Task column's values to return an integer number.  It first uses the INSTR function to get the position of the space character.  This, plus 1, is then used as the second argument of the MID function to return the remainder of the Task value after the space, i.e. the numeric characters, as a string expression.  Finally the VAL function returns this as a number data type which is capable of sorting numerically, unlike a string expression composed of numeric digits.

    In the WHERE clause the number returned by the above expression is then compared with the numbers in all other rows with the same Job # value, counting the rows where the number is equal to or greater than the number returned in the current row.  This gives the TimeRemaing value per row.

    The cumbersome expression could be avoided if the table contained sequential numbers of each task per Job #.  If you add a column named tasknumber, of long integer number data type (which is the default number data type) to the table, then the column can be populated with the following UPDATE query:

    UPDATE Tasks

    SET TaskNumber = VAL(MID(Task,INSTR(Task," ")+1));

    Which would number the tasks per job from 1 to 10 with the sample data you posted.  The query to return the TimeRemaining values can then be simplified to:

    SELECT [Job #], task, tasknumber, duration, start, finish,

    DSUM("Duration", "Tasks","[Job #] = """ & [Job #] & """

     AND tasknumber >= " & tasknumber)

     AS Timeremaining

    FROM Tasks

    ORDER BY [Job #],tasknumber;

    Domain functions like the DSum function are notoriously slow compared with SQL operators like the SUM operator.  The latter could be used in the query, but the result table would not be updatable, so the query would be unsuitable for use as a form's RecordSource property where you need to enter or edit data in the form.  An alternative would be to remove the DSum function call from the query altogether:

    SELECT [Job #], task, tasknumber, duration, start, finish

    FROM Tasks

    ORDER BY [Job #],tasknumber;

    and add an unbound text box control to the form, with a ControlSource property of:

    =DSum("Duration", "Tasks","[Job #] = """ & [Job #] & """ And TaskNumber >= " & [TaskNumber])

    The form will be a little slow to refresh the unbound text box as you scroll through records, but it should be acceptable.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2018-10-09T18:22:30+00:00

    Ken,

    That's for the response.  I tried your solution and strangely enough it works for small data set by not a large data set.  Small was 20 records and large was 1120 records.  With the large data set the field 'TimeRemaining' came back with an #Error response.

    Also would like to better understand the code you suggested to make sure I understand what it is doing if you have the time.  Thanks.

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2018-10-06T17:49:37+00:00

    The following, by calling the VBA DSum function, will return an updatable result table:

    SELECT [Job #], Task, Duration, start,Finish,

    DSUM("Duration", "Tasks","[Job #] = """ & [Job #] & """

     AND VAL(MID(Task,INSTR(Task,"" "")+1)) >= " & VAL(Mid(Task,INSTR(Task," ")+1)))

     AS TimeRemaining

    FROM Tasks

    ORDER BY [Job #],VAL(MID(Task,INSTR(Task," ")+1));

    where Tasks is the name of the table.  A far more efficient solution would be to join two instances of the table, but this would return a non-updatable result table.  For examples see Balances.zip in my public databases folder at:

    https://onedrive.live.com/?cid=44CC60D7FEA42912&id=44CC60D7FEA42912!169

    Note that if you are using an earlier version of Access you might find that the colour of some form objects such as buttons shows incorrectly and you will need to  amend the form design accordingly.  

    If you have difficulty opening the link, copy the link (NB, not the link location) and paste it into your browser's address bar.

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2018-10-06T16:26:58+00:00

    The example provided was only an example.  Each task I use is unique.  I do have a separate column called sequence number as you've suggested.  My real issue is having access calculate the running total in descending order.  Not only would I like to understand how to calculate the running total of the duration in descending order I'd also like to be able to alter the duration and have the running update.  

    So can the field 'time remaining' for task # 9 be the sum of task 10 duration + task duration 9?  And can the field 'time remaining' for task # 8 be the sum of task 10 duration + task duration 9 + task duration 8 and so on up to task #1?

    Was this answer helpful?

    0 comments No comments
  5. Tom van Stiphout 40,211 Reputation points MVP Volunteer Moderator
    2018-10-06T13:50:49+00:00

    One problem I can see is that the Task column is ascending to humans, but not to computers. They would sort:

    Task 1

    Task 10

    Task 2

    as you well know. So the question cannot be answered with the current data set, unless we go to extremes like splitting the number off the rest of the task name, but assuming this is sample data that would likely not work in the real world.

    You need to add a column TaskSequentialNumber. It does not have to go 1 -> 10, but it should be ascending (I would leave gaps of 10 so I can insert other tasks in the middle without renumbering).

    Was this answer helpful?

    0 comments No comments