A family of Microsoft relational database management systems designed for ease of use.
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.