Solved

UPDATE QUERY ( using multiple joins within  update statement)

Posted on 2006-07-02
4
839 Views
Last Modified: 2008-01-09
UPDATE Query

I have three tables;

Table A

InvT      Class

1      A1
2      A2
3      A3


Table B

Class      Class_id

A1      11
A2      22
A3      33

TABLE C

Invt      Class_id

1      NULL
2      NULL
3      NULL

I want a query to update the CLass_id field in the TABLE C...

So basically need to run through and link the invt( Table C ) to Table A ( by Invt )
then link to Table B  ( by Class )to get the Class_id I need...

Can this be done with an update query ?                  
0
Comment
Question by:I_J_Evans
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 84
ID: 17026698
Normally you don't store the values from other tables in a different table, instead you use the source tables and build a query which returns those values:

SELECT TableA.InvT, TableB.Class_id FROM TableA INNER JOIN TableB ON TableA.Class = TableB.Class

this would return the InvT from TableA and Class_id from TableB, which is (basically) what your TableC is showing ...

Not to say you don't sometimes need to store data from one table in another (the classic example is in an Ordering system - you alway store the Price of an item, even if that Price is stored elsewhere in the DB, since prices change during a products lifecycle) - but typically there is no need to do so, and storing data in several different places can result in different results during calculations.

Are these queries to be stored in the db? If so, you can build a "join" query to join TableA and TableB, then use that to update TableC:

[qryJoin]
SELECT TableA.InvT, TableB.Class_id
FROM TableA INNER JOIN TableB ON TableA.Class = TableB.Class;

Now build the Update query:

[qryUpdate]
UPDATE TableC SET TableC.Class_id = (SELECT TableB.Class_id FROM (TableA INNER JOIN TableB ON TableA.Class = TableB.Class) INNER JOIN TableC ON TableA.InvT = TableC.Invt);

Save both, then run qryUpdate to update TableC:

DoCmd.OpenQuery "qryUpdate"

You could probably combine these two into one.
0
 
LVL 84
ID: 17026704
Sorry, the SQL for qryUpdate in the above posting is incorrect. It should be:

[qryUpdate]
UPDATE TableC LEFT JOIN qryJoin ON TableC.Invt = qryJoin.InvT SET TableC.Class_id = qryJoin.Class_id;
0
 
LVL 84

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 125 total points
ID: 17026723
If you want to do this all in one SQL statement, this should work:

UPDATE TableC LEFT JOIN (TableA LEFT JOIN TableB ON TableA.Class = TableB.Class) ON TableC.Invt = TableA.InvT SET TableC.Class_id = [TableB].[Class_id];

You could save this as a query, or use this directly in VBA:

Currentdb.Execute "UPDATE TableC LEFT JOIN (TableA LEFT JOIN TableB ON TableA.Class = TableB.Class) ON TableC.Invt = TableA.InvT SET TableC.Class_id = [TableB].[Class_id]"

0
 
LVL 6

Expert Comment

by:maxy88
ID: 17026740

Try this:

UPDATE [Table C] AS H1,
(SELECT [Table B].[Class_id], [Table A].[InvT]
FROM [Table C] INNER JOIN ([Table B] INNER JOIN [Table A] ON [Table B].[Class]=[Table A].[Class]) ON [Table C].[InvT]=[Table A].[InvT]) AS H2
SET [H1].[Class_id] = [H2].[Table B].[Class_id]
WHERE [H1].[InvT]=[H2].[Table A].[InvT];

Max.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

733 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