Solved

Update Query - update same record multiple times

Posted on 2008-06-11
14
400 Views
Last Modified: 2011-09-20
Have the following Update Query:
UPDATE LINK INNER JOIN FORM_ID_358989923 ON LINK.TAG = FORM_ID_358989923.TAG SET FORM_ID_358989923.LOCATION = LINK!LOCATION;

In the LINK table, the Tag may appear several times, and this table is in chronological order, of when assets are moved.  TAG is the asset tag, and the link table might have multiple records for that Tag, if were moved several times.  The Update query runs, but  if the Tag is in more than one record, only the first instance gets updated.  Need it to update each time,  therefore the last time it appears in the Link Table should be what shows as updated in the FORM_ID table - please help.
0
Comment
Question by:dcorleto
  • 7
  • 7
14 Comments
 
LVL 44

Expert Comment

by:GRayL
ID: 21782152
Then you don't need each and every instance and they will continue to get written over.  Only the last remains, there, update just once on the Last.  How do you determine last?
0
 

Author Comment

by:dcorleto
ID: 21782840
Consider the following:
TAG        LOCATION
TAG01    A
TAG02    B
TAG03    C
TAG01    A
TAG04    D
TAG01    C

I am looking for the Update query to show The final locations for Tags 1,2,3 and 4. When the update is complete, Tag01 should show location C.  The records are scanned in with a handheld barcode reader  as the equipoment is moved and stored in the Link table.  
0
 
LVL 44

Expert Comment

by:GRayL
ID: 21788911
Yes, but how do you know that Location C is the 'last' location?  It could have just as easily moved from A to C to A.
0
 
LVL 44

Expert Comment

by:GRayL
ID: 21788916
I'm saying there must be a datetime or something similar associated with each record?
0
 

Author Comment

by:dcorleto
ID: 21793826
I could add a date/timestamp field easily, but for now, there is only the location and field and the records are entered in order of movement.
0
 
LVL 44

Accepted Solution

by:
GRayL earned 250 total points
ID: 21794634
To ensure that Jet selects the 'last' record, you need the datetime stamp.  Assuming the field is named DTStamp,

Select a.Tag, a.Location  FROM Link a WHERE a.Location In (SELECT Max(b.DTStamp) FROM Link b WHERE b.Tag = a.Tag AND b.Location = a.Location);
0
 

Author Comment

by:dcorleto
ID: 21794795
I'm going to give that a try - will let you know - thank you so much.
0
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

Author Comment

by:dcorleto
ID: 21794938
do I not use the Update statement?
0
 
LVL 44

Expert Comment

by:GRayL
ID: 21795005
No, once you run the UPDATE, you then need to run the SELECT statement to locate the location with the 'last' - most recent - Max dtStamp.

Remember, Actions queries like SELECT INTO, INSERT INTO, UPDATE, etc. do not return record sets.
0
 

Author Comment

by:dcorleto
ID: 21795026
So can I still run this as an Update Query from Access or do I have to use VBA?
0
 
LVL 44

Expert Comment

by:GRayL
ID: 21795129
I'm supposing you already have the UPDATE query saved under a name something like qudTags.  You need to now create a new query using the SQL I gave you above which you should save as something like qryGetLastLocation.  Are you OK with that?
0
 

Author Comment

by:dcorleto
ID: 21821141
Still not working

For your statement,

Select a.Tag, a.Location  FROM Link a WHERE a.Location In (SELECT Max(b.DTStamp) FROM Link b WHERE b.Tag = a.Tag AND b.Location = a.Location);

can your tell me what "a." and "link a" mean?  I must have the syntax wrong.
0
 

Author Comment

by:dcorleto
ID: 21822085
Dear GRayL:
I think I'm getting there but now get a data type mismatch in criteria error.  All the data types match in the two tables.  Any suggestions?

SELECT a.TAG, a.LOCATION
FROM FORM_ID_398779041 AS a
WHERE (((a.LOCATION) In (SELECT Max(b.TimeStamp) FROM FORM_ID_398779041 b WHERE b.Tag = a.Tag AND b.Location = a.Location)));
0
 
LVL 44

Expert Comment

by:GRayL
ID: 21839327
I was away the past three days.  While you accepted a solution and I was awarded a B, I would have preferred to work out the answer.  I see the 'dumb' mistake:

SELECT a.TAG, a.LOCATION
FROM FORM_ID_398779041 AS a
WHERE (((a.TimeStamp) In (SELECT Max(b.TimeStamp) FROM FORM_ID_398779041 b WHERE b.Tag = a.Tag AND b.Location = a.Location)));


That any better?
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now