![]() |
|
|||||||
![]() |
|
Microsoft Access question?
|
![]() |
LinkBack | Thread Tools | ![]() |
Display Modes | ![]() |
|
|||
|
How can I write a query in Microsoft Access that references the row above it?
Specifically, I have a table that looks like this: 5 -7 3 6 I want to write a query that will calculate a running total, but will never go negative. So the results would be: 5 0 3 9 Here's a bigger example. I have a table like this: Row 1: 7 Row 2: 1 Row 3: -3 Row 4: -6 Row 5: -2 Row 6: 9 Row 7: 10 And I want to write a query that will keep a running total, but will never go negative. Like this: Row 1: 7 Row 2: 8 Row 3: 5 Row 4: 0 Row 5: 0 Row 6: 9 Row 7: 19 I don't want the absolute values. I want negative numbers to decrease the total. I just don't want them to decrease it below 0. |
| Links |
|
|
|
|||
|
You posted this in Software already.
|
|
|||
|
For the second question on running totals:
SELECT SUM(ABS(FIELD1))) AS CALCTOTAL FROM X GROUP BY ABS(FIELD1) On your first question on how to reference the row above it is not possible in MS Access unless you write VBA code. The problem is that you are probably using simple SQL statements. To be able to use prior rows in SQL you need to use CURSOR programming in T-SQL (transact sql), which means using stored procedures in MS SQL Server. If you use VBA in MS-Access you can mimic what CURSORS do in stored procedures, but you need to know how to code and read a record at a time. Now, your question is not that clear. It might be possible to do it without programming and using multiple controls, but I don't have enough information to know what you are trying to achieve, especially no information on the data like the primary and foreign keys to link the data. |
|
|||
|
I am not quite sure exaclty what you are asking. If you are talking about referencing a row above another row in a table, I am not sure why you would want to that, or that it would be a reliable way to go, as the order of the rows in the table is subject to change.... that being said -
When you say you want to write a query, I am thinking that what you really need (or are talking about) is a method or macro. But I don't get the whole "running total" thing. Do you have a program that runs, and users input data, and you want to keep a running total of what they enter? I think I would need more info to give you an answer. |
![]() |
| Thread Tools | |
| Display Modes | |
|
|