It depends. You can't do it with straight SQL at all. You can do it by calling a function that accumulates the sum in a Static variable, but it only works if you read the records sequentially in forward order.
Here's a sample function that does this:
Code:
Public Function RunningSum(Value As Variant) As Long
Static lngSum As Long
If IsEmpty(Value) Then
lngSum = 0
Else
lngSum = lngSum + Value
End If
RunningSum = lngSum
End Function
To use it, you need to execute the statement
RunningSum Empty first, to reset lngSum to zero, then run the query.
In your query, you add a calculated field with the expression
RunningSum(Col2). This adds the current value to the previous running sum and returns the result.
Like I said, this only works for a recordset read sequentially forward. What this means is that if you open the query directly in the user interface, or open a form based on the query, the values will get all screwed up as soon as you start scrolling upward/backward or jump to another record, etc. This happens because these operations cause Access to read the records out of sequence. However, the function
will work if you read a forward-only recordset based on the query.
You said you want to display the values, and it sounds like I just said you can't do that, but actually there's a way to make it work: Change the query to a MakeTable query that builds a temporary table. Run it, then open the temporary table to display the results. (Of course, you wouldn't want to allow any updating in the temporary table.)
Rick Sprague
Want the best answers? See faq181-2886
To write a program from scratch, first create the universe. - Paraphrased from Albert Einstein