Go Back   Talk Microsoft > Microsoft Operating Systems & Software > Microsoft Office Family

Reply
     Microsoft Access question?  
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-22-2007, 08:00 AM
Junior Member
 
Join Date: Nov 2007
Posts: 6
Default Microsoft Access question?

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.
Reply With Quote
Links
  #2 (permalink)  
Old 11-22-2007, 09:34 AM
Junior Member
 
Join Date: Nov 2007
Posts: 3
Default

You posted this in Software already.
Reply With Quote
  #3 (permalink)  
Old 11-22-2007, 11:38 AM
Junior Member
 
Join Date: Sep 2007
Posts: 8
Default

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.
Reply With Quote
  #4 (permalink)  
Old 11-22-2007, 12:14 PM
Junior Member
 
Join Date: Sep 2007
Posts: 12
Default

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.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 12:47 AM.