MS Access VBA SQL - Inserting a record from one table into another table

六眼飞鱼酱① 提交于 2020-05-23 10:24:16

问题


Hello Stackoverflow community,

Below is the code I have in Access VBA as an on-click button on a userform. As you can tell I have very limited knowledge in VBA & SQL but trying to revise one of my companies databases. I have two duplicate tables (PrintTable & ManPowerCalculator) and I am trying to insert every item from ManPowerCalculator table into the PrintTable where the EmplID input box on the userform = that within the ManPowerCalculator Table.

CurrentDb.Execute "INSERT INTO PrintTable VALUES (*) SELECT (*)FROM ManPowerCalculator WHERE EmplID # = " & Me.EmplID "

Also I am not defining any variables, maybe it would make it more efficient. Thank you for your time in reading through this.


回答1:


This should do the job:

CurrentDb.Execute "INSERT INTO PrintTable SELECT * FROM ManPowerCalculator WHERE EmplID = " & Me.EmplID

Note that this will only work if the PrintTable and ManPowerCalculator tables have the exact same fields. If they don't, you will have to specify the field names both in the INSERT and SELECT parts of the query.

This is done in the following format:

CurrentDb.Execute "INSERT INTO PrintTable (x, y, z) SELECT x, y, z FROM ManPowerCalculator WHERE EmplID = " & Me.EmplID

With x, y and z being possible field names.



来源:https://stackoverflow.com/questions/41832815/ms-access-vba-sql-inserting-a-record-from-one-table-into-another-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!