Solved

update and insert records

Posted on 2009-05-09
4
219 Views
Last Modified: 2013-11-27
Hello.
I would like to be able to update existing records and insert new records into a main table from another table.

Is this possible using append query to do the inserts and update query to change the data on existing records?

I have been trying to do an append query to append new records into the main table if they don't already exist another table. Then doing an update query to update the existing data in the main table?

How may this be done or is it better doing it with a macro or code

Thanks,
Ivan
0
Comment
Question by:icarey
  • 2
  • 2
4 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24343361
you have to do this with 2 queries.
first, the update based on the join for the existing ones, and then insert those that are not yet in the table.
the update should be easy.
the insert requires a select like this one:
select b.*
  from table b
  left outer join a on (a.key = b.key) 
   where a.key is null

Open in new window

0
 
LVL 3

Author Comment

by:icarey
ID: 24343580
thanks angelIII

I have created the query ok but am unable to insert into the table due to field count

Tables STOCK and stock_new
Field names in both
NAME TITLE NAME2

SELECT stock_new.*
FROM stock_new LEFT JOIN STOCK ON stock_new.NAME2 = STOCK.NAME2
WHERE (((STOCK.NAME2) Is Null));

displays the new records

INSERT INTO STOCK(NAME,TITLE,NAME2)
SELECT stock_new.*
FROM stock_new LEFT JOIN STOCK ON stock_new.NAME2 = STOCK.NAME2
WHERE (((STOCK.NAME2) Is Null));

comes up with an error
Number of query values and destination fields are not the same

0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 24343616
this will do:
INSERT INTO STOCK(NAME,TITLE,NAME2)

SELECT stock_new.Names2, stock_new.Title, stock_new.Names2

FROM stock_new LEFT JOIN STOCK ON stock_new.NAME2 = STOCK.NAME2

WHERE (((STOCK.NAME2) Is Null));

Open in new window

0
 
LVL 3

Author Closing Comment

by:icarey
ID: 31579754
Thank you angelIII your answer has help me greatly

Ivan
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

920 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

12 Experts available now in Live!

Get 1:1 Help Now