Solved

UPDATE QUERY ( using multiple joins within  update statement)

Posted on 2006-07-02
4
803 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
  • 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

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…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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 …

707 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

19 Experts available now in Live!

Get 1:1 Help Now