Solved

UPDATE QUERY ( using multiple joins within  update statement)

Posted on 2006-07-02
4
847 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 85
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 85
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 85

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

As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

717 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