简体   繁体   中英

How can I append records (specific rows, with multiple (but not all) columns) from one table to another

Each day ~5000 records are uploaded to tblRecordsCurrent, at some point within the next few days when those records have been processed, they need to be moved to tblRecordsHistorical. Each record has a Foreign Key DataSetID that ties it to the date/time it was uploaded (Parent Table).

How, within vba, can I insert a single DataSet of tblRecordsCurrent into the tblRecordsHistorical from tblRecordsCurrent. I cannot insert all columns as both tables contain persisted columns.

I can't put the entire INSERT INTO tblRecordsHistorical A, B, C, D, E, F... as it is too long for access vba.

Any ideas?

If you save the query in your Access database, then you can execute in in VBA the following way:

DoCmd.OpenQuery "yourQueryName", acViewNormal, acEdit

Or

CurrentDb.OpenRecordset("yourQueryName")

Or

CurrentDb.Execute "qryAddLoginfoRow"

This allows you to execute the query without having the query stored in your VBA code. But you are also able to execute this via a query in VBA :

INSERT INTO tblRecordsHistorical (col1, col2...) 
SELECT col1, col2...
FROM tblRecordsCurrent

EDIT:

You can create a long SQL string by concatenating the string together:

SQLString = "INSERT INTO tblRecordsHistorical (col1, col2...) " & _
            " SELECT ... " & _
            " FROM tblRecordsCurrent "

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