473,768 Members | 5,334 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 4003
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
4236
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
2625
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
7748
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 incomplete record ("is null" in delivery date)], but I need to re-use the job no. if the job is...
9
2878
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 previous record. For the first record in the dynaset, I'll settle for a value = 30 seconds. For the...
1
3828
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 function in various ways but cannot get it to work. The date field is in the format dd/mm/yyyy. Staff...
1
1491
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
5018
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 than the previous entry's number to keep it unique (I don't want autonumbering for this, as I...
2
3387
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 table, a simple two field table with an ID field and a field of consecutive numbers in consecutive...
1
4341
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 why. Can anyone out there help? Thanks Repeat value of previous record field into current...
0
9576
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9407
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9843
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8840
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7384
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6656
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5283
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3932
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
2
3534
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.