如何插入“整个”使用 VBA 将 DAO 记录集转换为表
我有一个 DAO 记录集,可以很好地创建,我可以将记录从该集传输到表中,这是逐行完成的并且效果很好,但是我一次传输大量数据,因此这可能需要很长时间一行一行。
有没有一种方法可以一次性传输整个记录集,而不是逐行传输
请参阅下面的当前使用的代码 -
Dim SendE1 As DAO.Recordset
Set SendE1 = CurrentDb.OpenRecordset("SELECT TBL_ImportTable.* FROM TBL_ImportTable", dbOpenDynaset)
SendE1.MoveLast
Do Until SendE1.EOF
sqlinsert = "INSERT INTO TBL_E1Jobs (StartDate, StartTime, EndDate, EndTime, Location, UserID, WorkStationID, DocumentNumber, E1Shift, OperSeq, Facility, AdjustedforShifts, WeekNum)" & _
" VALUES ('" & SendE1("StartDate") & "', '" & SendE1("StartTime") & "', '" & SendE1("EndDate") & "', '" & SendE1("EndTime") & "', '" & SendE1("Location") & "', '" & SendE1("UserID") & "', '" & SendE1("WorkstationID") & "', '" & SendE1("DocumentNumber") & "', '" & SendE1("E1Shift") & "', '" & SendE1("OperSeq") & "', '" & SendE1("Facility") & "', '" & SendE1("AdjustedforShifts") & "', '" & SendE1("WeekNum") & "') "
DoCmd.RunSQL (sqlinsert)
SendE1.MoveNext
Loop
SendE1.Close
Set SendE1 = Nothing
I have a DAO recordset that gets created fine and I can transfer the records from the set to a table, this is done row by row and works well but I am transfering a large amount of data at once so this can take a very long time row by row.
Is there a way to transfer the ENTIRE recordset in one go, rather than row by row
See below for current code in use -
Dim SendE1 As DAO.Recordset
Set SendE1 = CurrentDb.OpenRecordset("SELECT TBL_ImportTable.* FROM TBL_ImportTable", dbOpenDynaset)
SendE1.MoveLast
Do Until SendE1.EOF
sqlinsert = "INSERT INTO TBL_E1Jobs (StartDate, StartTime, EndDate, EndTime, Location, UserID, WorkStationID, DocumentNumber, E1Shift, OperSeq, Facility, AdjustedforShifts, WeekNum)" & _
" VALUES ('" & SendE1("StartDate") & "', '" & SendE1("StartTime") & "', '" & SendE1("EndDate") & "', '" & SendE1("EndTime") & "', '" & SendE1("Location") & "', '" & SendE1("UserID") & "', '" & SendE1("WorkstationID") & "', '" & SendE1("DocumentNumber") & "', '" & SendE1("E1Shift") & "', '" & SendE1("OperSeq") & "', '" & SendE1("Facility") & "', '" & SendE1("AdjustedforShifts") & "', '" & SendE1("WeekNum") & "') "
DoCmd.RunSQL (sqlinsert)
SendE1.MoveNext
Loop
SendE1.Close
Set SendE1 = Nothing
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
@ularis 是正确的。执行此操作的正确方法是使用 SQL 查询。阅读您对他的回答的评论后,您可以采取一些步骤来避免擦除尚未复制的数据:
请注意,如果您在链接的 ODBC 表上运行这些查询,则需要包含 dbSeeChanges 选项(即
dbFailOnError + dbSeeChanges
)。@cularis is correct. The right way to do this is in a SQL query. Having read your comments to his answer, there are a few steps you can take to avoid wiping out data that has not been copied:
Please note that if you run those queries on linked ODBC tables, you will need to include the
dbSeeChanges
option (ie,dbFailOnError + dbSeeChanges
).不是 DAO,而是 SQL 解决方案,可以满足您的需求:
插入...选择 MSDN
Not a DAO, but a SQL solution, that does what you need:
INSERT INTO ... SELECT MSDN