Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

UPDATE QUERY ( using multiple joins within  update statement)

Posted on 2006-07-02
4
Medium Priority
?
858 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 500 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

604 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