简体   繁体   中英

MS Access SQL - Insert Query while adding an additional column into table

I am using VBA to insert a queryset into an existing ms access 365 table with an SQL statement.

Code to insert query (working):

 CurrentDb.Execute "Insert Into Tablename Select temp.* From temp",dbFailOnError 'if table exists
 
 CurrentDb.Execute "Select temp.* Into Tablename From temp",dbFailOnError 'if table does not exist

But now, I am also trying to add values to a column not in the query (a log number) into the table for the inserted queryset. So lets say insert '1' into a column called Evtlog in the table for these records.

What I was hoping to do to add the log number into the column Evtlog (gives syntax error):

CurrentDb.Execute "Insert Into Tablename Select temp.* From temp Set Evtlog=1",dbFailOnError 

But I get an error on this statement, is there anyway to combine "Select" with "Set"?

Preferably, I would prefer to use Select as the query columns can change, but I am open to other solutions - for example, can a column be added to the queryset?

Try this:

CurrentDb.Execute "Insert Into Tablename Select temp.*, 1 As EvtLog From temp",dbFailOnError 'if table exists
 
CurrentDb.Execute "Select temp.*, 1 As EvtLog Into Tablename From temp",dbFailOnError 'if table does not exist

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM