gpt4 book ai didi

ms-access - 如何使用VBA将 "Entire"DAO记录集插入到表中

转载 作者:行者123 更新时间:2023-12-02 23:15:15 27 4
gpt4 key购买 nike

我有一个 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

最佳答案

@ularis 是正确的。执行此操作的正确方法是使用 SQL 查询。阅读您对他的回答的评论后,您可以采取一些步骤来避免删除尚未复制的数据:

Dim db As DAO.Database, RecCount As Long

'Get the total number of records in your import table to compare later
RecCount = DCount("*", "TBL_ImportTable")

'This line is IMPORTANT! each time you call CurrentDb a new db object is returned
' that would cause problems for us later
Set db = CurrentDb

'Add the records, being sure to use our db object, not CurrentDb
db.Execute "INSERT INTO TBL_E1Jobs (StartDate, StartTime, ..., WeekNum) " & _
"SELECT StartDate, StartTime, ..., WeekNum " & _
"FROM TBL_ImportTable", dbFailOnError

'db.RecordsAffected now contains the number of records that were inserted above
' since CurrentDb returns a new db object, CurrentDb.RecordsAffected always = 0
If RecCount = db.RecordsAffected Then
db.Execute "DELETE * FROM TBL_ImportTable", dbFailOnError
End If

请注意,如果您在链接的 ODBC 表上运行这些查询,则需要包含 dbSeeChanges 选项(即 dbFailOnError + dbSeeChanges)。

关于ms-access - 如何使用VBA将 "Entire"DAO记录集插入到表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6843017/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com