使用 VBA 使用另一个表中的值更新访问表 [英] Update access table with values from another table using VBA
本文介绍了使用 VBA 使用另一个表中的值更新访问表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在一个数据库中有两个表,表 1 和表 2.我正在尝试根据表 2 中的数据使用 VBA 代码更新表 1.
I have two tables, table 1 and table 2, in a database. I am trying to update table 1 using VBA code based on data in table 2.
示例:
表一
PartNo Price Description
--------------------------
A 100
B 200 Bad
C 300
表 2
PartNo Price Description
--------------------------
A 700
B 200 Good
D 900 Used
更新后,table1 应该使用那些已经改变的价格或描述进行更新,其中 table1 PartNo = table 2 PartNo,并添加表 2 中存在的任何新项目.
After the update, table1 should be updated with those prices or descriptions that have changed where table1 PartNo = table 2 PartNo , and add any new items that were present in table 2.
表一
PartNo Price Description
--------------------------
A 700
B 200 Good
C 300
D 900 Used
我似乎不太正确,感谢您的帮助.
I can't seem to get it quite right, appreciate the help.
推荐答案
你可以用两条语句来完成,一个更新和一个插入,如下所示:
You can do it with two statements, an update and an insert like this:
更新:
UPDATE Table1
INNER JOIN table2
ON(table1.partNo = table2.PartNo)
SET table1.price = table2.price,
table1.description = table2.description
然后插入:
INSERT INTO table1 (PartNo,Price,Description)
SELECT PartNo,Price,Description FROM table2 t
WHERE NOT EXISTS(SELECT 1 FROM table1 s
WHERE t.PartNo = s.PartNo)
这篇关于使用 VBA 使用另一个表中的值更新访问表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文