473,585 Members | 2,689 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to create query with calc'd field that equals value of [Field] in previous record + value of [AnotherField] in current record?

MLH
Fields in MyTable:

PostID
PostDate
RollQtyXfer
RollDenominatio n
RollCount37
RollCount23
PostID is an A97 AutoNumber field. PostDate is a Date field.
The rest of the fields are Integer type.

This is what I want to see in a query:
[PostID] [PostDate] [RollQtyXfer] [RollCount37] [CalcValue]
where [PostID]>1 and CalcValue is a calculated field that
equals [RollCount37] in the PREVIOUS record + [RollQtyXfer]
value in the current record.

Is it possible?
Apr 7 '06 #1
2 3994
something like this is doable using Transact sql in Sql server, but
Access doesn't support that kind of operation. Here is a sample of the
tsql - then I explain how you do this in Access

create table #temp1(
rowId int Identity(1,1),
fld1 int,
fld2 int)

insert into #temp1(fld1, fld2)
select 1, 2 union
select 3, 4 union
select 5, 6 union
select 7, 8 union
select 9, 10

select t1.fld1, t1.fld2, t2.fld1 + t2.fld2 from #temp1 t1 join #temp1 t2
on t1.rowid = t2.rowID + 1

You are using a self join here where the first table is listing the rows
that you have, but the second copy is listing the next set of rows - and
you join the ID field by tricking it to count the next ID (rowID + 1).
You can't do this in Access though. Instead, in Access you create a 2nd
copy of the table and populate it with the next set of rows (or previous
set of rows - whatever - offset by one row) and join the ID fields as in
the example above, then do a basic query.

Rich

*** Sent via Developersdex http://www.developersdex.com ***
Apr 7 '06 #2
MLH <CR**@NorthStat e.net> wrote in
news:vh******** *************** *********@4ax.c om:
Fields in MyTable:

PostID
PostDate
RollQtyXfer
RollDenominatio n
RollCount37
RollCount23
PostID is an A97 AutoNumber field. PostDate is a Date field.
The rest of the fields are Integer type.

This is what I want to see in a query:
[PostID] [PostDate] [RollQtyXfer] [RollCount37]
[CalcValue] where [PostID]>1 and CalcValue is a calculated
field that equals [RollCount37] in the PREVIOUS record +
[RollQtyXfer] value in the current record.

Is it possible?


You can do this by creating a parameterized subquery that
returns the value of rollCount37 for the previous record and
embedding that in hte main query as a calculated field.

We'll start with
SELECT top 1 rollcount37
from mytable ALIAS st
WHERE st.postdate < mn.postdate
ORDER by postdate.desc

If the parameter (mn.postdate) is the same as the field value in
the main query, it will use that value.

and embed that in the next query
SELECT postID,
Postdate,
rollqtyXFER,
RollCount37,
(SELECT top 1 rollcount37
from mytable ALIAS st
WHERE st.postdate < mn.postdate
ORDER by postdate.desc
) as CalcValue
FROM mytable alias mn
Order by postdate;

Note I havent run this SQL so check for typos.
--
Bob Quintal

PA is y I've altered my email address.
Apr 7 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
4230
by: Stefan Goerres | last post by:
Hello, I have put a function myfunc(val1, val2) which I call in a query. So far I only have arguments from the actual row there is no problem. But I can't make it working that i use a value from the next/last row? Is there any idea out there? Thanks Stefan
2
2618
by: Steve | last post by:
Does any anyone have a procedure for a query where a calculated field returns the previous record's value in another field. For example: A F A Z F M Z The primary key is in random order in the query and the letters ar not sorted.
4
7739
by: Apple | last post by:
1. I want to create an autonumber, my requirement is : 2005/0001 (Year/autonumber), which year & autonumber no. both can auto run. 2. I had create a query by making relation to a table & query, but I can't update record in query or in form. I believe the problem is due to the source query. In source query, there is a filter to show the...
9
2867
by: MLH | last post by:
I have a database (datatrek.mdb) with a table named DATA. The table has a date/time field with default value = Now(). It has 100 records in it entered over a 50-minute period. I would like the query to display 100 records with a new, calculated field showing timelapse between time of record entry of current record and time of entry of...
1
3822
by: ????? | last post by:
I have a query which shows the following fields and orders by every one in turn from left to right in ascending order. The fields are DATE, , WORK, *, DESCRIPTION and . How can I get a running total of the * field? I need to use every single field to find whether a record occurs before the current record. I have tried using the DSum...
1
1478
by: pbartel | last post by:
I want to auto fill a feild from a previous record. Pressing f8 does the trick but I want to automate it?
6
5009
by: Dixie | last post by:
I have asked this question before, but I could not get the suggested solution work. So I will give more details this time. I have an append query that adds several hundred records to a table into a text field. Next to this text field is a separate Number (Single) field with a unique ID number for the entry. I want each number to be 1 more...
2
3378
by: Wade | last post by:
I am pretty new to Access, especially writing code, but I found code on the web to do just what I want and it is posted below. It returns a field value from a previous record. I found the code at: http://support.microsoft.com/?kbid=210504; many of you have probably seen this code. I have an Access form and a query, both based on exactly the same...
1
4331
by: roveagh1 | last post by:
Hi I've been using the 2 year old link below to repeat values from previous record field into current corresponding field. It's worked fine for text but the last piece of advice was to use the same logic for a date field. i.e. theValue.defaultvalue = "#" & theValue.value & "#" I can't get this to work for some reason and I can't figure out...
0
7900
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7832
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8192
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8332
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8204
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6592
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5382
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3853
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2338
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.