问题
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