473,398 Members | 2,165 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

How can I retrieve a previous record based on multiple variables?

I'm trying to create a field in a Query which essentially looks up "yesterday's" performance for a "specific location". I've read other blogs that suggest a DLookup; however it is being based on an auto number Pimary Key. This will not yeild the correct field as each row is not necessarily in chronological order.

To simplify - I have 3 fields [LOCATION],[DATE],[UNITS OPEN]There are 6 different locations of which i'm keeping history. Does anyone know of an elaborate expression that will match up Location-1(of 6) with Location-1(of 6) and display the prior day's [UNITS OPEN]?

Thanks in advance for any assistance!
Jan 5 '12 #1

✓ answered by Rabbit

There's not much to elaborate on. You just have to convert what I said into the SQL syntax. Something like this:
Expand|Select|Wrap|Line Numbers
  1. SELECT *
  2. FROM someTable AS T1
  3. LEFT JOIN someTable AS T2
  4. ON T1.Location = T2.Location
  5.    AND (T1.DateField - 1) = T2.DateField
Where the LEFT JOIN is when I said "join the table to itself" and everything after ON is when I said "on the location and date to prior date"

11 1948
Rabbit
12,516 Expert Mod 8TB
You can join the table to itself on the location and date to prior date.
Jan 5 '12 #2
Can you elaborate a bit more?
Jan 5 '12 #3
Rabbit
12,516 Expert Mod 8TB
There's not much to elaborate on. You just have to convert what I said into the SQL syntax. Something like this:
Expand|Select|Wrap|Line Numbers
  1. SELECT *
  2. FROM someTable AS T1
  3. LEFT JOIN someTable AS T2
  4. ON T1.Location = T2.Location
  5.    AND (T1.DateField - 1) = T2.DateField
Where the LEFT JOIN is when I said "join the table to itself" and everything after ON is when I said "on the location and date to prior date"
Jan 5 '12 #4
I think i'm getting it, only now an error message appears -
The specified field "" could refer to more than one talbe listed in the From clause of your SQL statement.

When i joined the table to itself, i basically added the same query "MOVEMENT Query" which then renamed itself "MOVEMENT Query_1" I joined the two location fields, then in SQL view i populated the syntax you suggested replacing T1 & T2 with the appropriate table & field names. Any idea what i'm doing wrong?
Jan 6 '12 #5
Rabbit
12,516 Expert Mod 8TB
You're probably attempting to include a field from the table without qualifying which table to use. Since you have two table with the same field names, you have to tell it which one you want.
Jan 6 '12 #6
Okay I've corrected the last issue. Now when i apply the "AND (T1.DateField - 1) = T2.DateField" Syntax, the fields go blank.
Jan 9 '12 #7
Rabbit
12,516 Expert Mod 8TB
You're probably picking fields from the right side of the join instead of the left side of the join. It's hard to say without seeing any of the code.
Jan 9 '12 #8
NeoPa
32,556 Expert Mod 16PB
IE. If you post the code you're using, along with the explanation, then we are in a much better position to be able to help you. Currently we are not in a good position at all of course due to lack of relevant information.
Jan 9 '12 #9
Hey guys... USER ERROR. When i created the new (duplicate) table, i forgot to correct the title of my joins. The new "yestereday's" field is working perfectly. THANK YOU!!!!

One last question. After manually updating the code in SQL view, I am no longer able to view my query in Design View? Is that normal?
Jan 9 '12 #10
Rabbit
12,516 Expert Mod 8TB
Sometimes, the SQL can't be graphically represented. In this case, it is because of the complex join condition.
Jan 9 '12 #11
NeoPa
32,556 Expert Mod 16PB
Adkolta:
After manually updating the code in SQL view, I am no longer able to view my query in Design View? Is that normal?
This is usually the case when an element of SQL has been introduced that is simply not supported by the graphical interface. An examples of this would be use of the UNION keyword. Such queries cannot be represented graphically by Access so it won't allow switching to graphical mode. Typically though, once SQL code has been updated it can be switched between SQL and graphical mode at will.
Jan 9 '12 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Derek Cooper | last post by:
I hope you can help me. I posted this in the microsoft sql server newsgroup a few days ago and got no response so I thought I'd try here. If I can provide any clarification I'll be glad to do so....
9
by: Karl Roes | last post by:
Hi All, I would like some advice on Next / Previous record buttons. I have a main form for the client, and a continuous subform listing client transactions. If I open one of these transactions...
1
by: matthewemiclea | last post by:
I have a subform, where in the "on current" event, I display code that is supposed to recognize which record I am pointing to, and then display more specific info in another subform based on that...
0
by: Takeadoe | last post by:
First, let me say that I'm brand new to Access, so please assume I know nothing. I've got a table (210k records) of deer harvest information. Date of harvest is one of many variables in the table....
0
by: KelHemp | last post by:
Greetings, I've been using this site for lots of access help in the past, and it's very helpful! I have a new complexity for you all. Reworking a form to record 70-80 years of oil production on...
1
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...
9
by: SuzK | last post by:
In Access 2002, I have created a Make Table query to add a new field “Base$” through a custom function, moving average, that averages each 8 weeks of data. However, I also need to calculate a...
16
by: zoeb | last post by:
Hi, I am a complete novice to Access VBA and looking for some help to select a record. I am looking to perform an operation on the previous record - i.e. adding a new blank field, and then...
5
by: usr123 | last post by:
I am trying to get the previous record from a table. Scenario is: I have a previous button on the form. User browse through the records one by one to go back/forward. For first time to pull data...
1
by: usr123 | last post by:
I want to get the previous record based upon ID which is PK of the table. Users are allowed to delete records from the table. Problem: Coz ID is autonumber,every time new record is created,it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
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...
0
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...

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.