将完整的 ADO 记录集插入现有的 ACCESS table WITHOUT LOOP
insert full ADO Recordset into existing ACCESS table WITHOUT LOOP
我的 VBA 模块中有一个填充的 ADO 记录集。我在 ACCESS 中也有一个 table,它与记录集的结构完全相同。
现在我使用遍历每个数据集记录的循环(这很好)填充 table。
我想知道的是:有没有办法将整个记录集插入访问 table? (更重要的是:这会不会显着加快)
没有。 GetRows
.
方法没有反向等价物 - 可能是 SetRows
这是一个基本示例(在本例中来自 excel 的 运行),说明了如何使用断开连接的记录集来添加记录。
Sub Tester()
Dim con As ADODB.Connection, rs As ADODB.Recordset
Dim i As Long
Set con = getConn()
Set rs = New ADODB.Recordset
rs.CursorLocation = adUseClient '<<<< important!
'get an empty recordset to add new records to
rs.Open "select * from Table1 where false", con, _
adOpenDynamic, adLockBatchOptimistic
'disconnect the recordset and close the connection
Set rs.ActiveConnection = Nothing
con.Close
Set con = Nothing
'add some new records to our test recordset
For i = 1 To 100
rs.AddNew
rs("UserName") = "Newuser_" & i
Next i
'reconnect to update
Set con = getConn()
Set rs.ActiveConnection = con
rs.UpdateBatch '<<< transfer to DB happens here: no loop!
rs.Close
'requery to demonstrate insert was successful
rs.Open "select * from Table1", con, _
adOpenDynamic, adLockBatchOptimistic
Do While Not rs.EOF
Debug.Print rs("ID").Value, rs("UserName").Value
rs.MoveNext
Loop
rs.Close
con.Close
End Sub
Function getConn() As ADODB.Connection
Dim rv As New ADODB.Connection
Dim strConn As String
strConn = "Provider=Microsoft.ACE.OLEDB.12.0;" _
& "Data Source = " & ThisWorkbook.Path & "\Test.accdb"
rv.Open strConn
Set getConn = rv
End Function
VBA 记录集虚拟存在于运行时调用的内存中,直到它们包含在实际的物理格式中(即 csv、txt、xlsx、xml、数据库临时文件 table)保存到硬盘。这类似于 R 或 Python pandas、SAS 数据集、PHP 数组和其他数据结构中的数据帧。
考虑使用 CopyFromRecordset methods into an Excel spreadsheet to be saved as csv, txt, xlsx, or xml. Alternatively, you can use the Save 方法以这种格式导出 ADO,以将记录集保存为持久格式类型,如 xml。
然后,将生成的文件附加到 MS Access table 及其自动数据迁移功能:
- 对于电子表格:
DoCmd.TransferSpreadsheet
- 对于 txt、csv 或其他带分隔符的文件:
DoCmd.TransferText
- xml 个文件:
Application.ImportXML
- 对于本地或ODBC/OLEDB链接数据库tables:
INSERT INTO
追加SQL查询
要使用 SQL 语句完成此操作,请使用 SELECT/INSERT... IN [Designate DB A; record posted to] or FROM... IN [指定 DB B;记录原始出处]
您只能在单个查询中使用一次 IN 语句。因此,您使用 ADODB 连接创建另一个连接以确定另一个源连接。
Function example()
Dim dB_External As String
Dim db_Local As String
Dim cnLocal As ADODB.Connection
Dim cnExternal As ADODB.Connection
Set cnLocal = CurrentProject.Connection
Set cnExternal = New ADODB.Connection
cnExternal .Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\...accdb;Persist Security Info=False;"
dB_External = "C:\Users\...accdb"
db_LOCAL = "C:\Users\...accdb"
Example A:
strSQL = "INSERT INTO *Local table to receive records* (Column Designations)"
strSQL = strSQL & " SELECT ( *Corresponding records from external table* )"
strSQL = strSQL & " FROM *External table name* IN '" & dB_External & "'"
cnLocal.Execute (strSQL)
我使用上面的代码,如果我 select 从单个外部 table.
使用本地 ADODB 连接
Example B:
strSQL = "INSERT INTO *Local table to receive records* (Column Designations) IN '" & dblocal & "'"
strSQL = strSQL & " ( *Corresponding records from external table* )"
strSQL = strSQL & " FROM *External table name*
cnExternal.Execute (strSQL)
我使用上面的代码使用外部 ADODB 连接,如果我 select 涉及在外部数据库中加入多个 table。
我的 VBA 模块中有一个填充的 ADO 记录集。我在 ACCESS 中也有一个 table,它与记录集的结构完全相同。
现在我使用遍历每个数据集记录的循环(这很好)填充 table。
我想知道的是:有没有办法将整个记录集插入访问 table? (更重要的是:这会不会显着加快)
没有。 GetRows
.
SetRows
这是一个基本示例(在本例中来自 excel 的 运行),说明了如何使用断开连接的记录集来添加记录。
Sub Tester()
Dim con As ADODB.Connection, rs As ADODB.Recordset
Dim i As Long
Set con = getConn()
Set rs = New ADODB.Recordset
rs.CursorLocation = adUseClient '<<<< important!
'get an empty recordset to add new records to
rs.Open "select * from Table1 where false", con, _
adOpenDynamic, adLockBatchOptimistic
'disconnect the recordset and close the connection
Set rs.ActiveConnection = Nothing
con.Close
Set con = Nothing
'add some new records to our test recordset
For i = 1 To 100
rs.AddNew
rs("UserName") = "Newuser_" & i
Next i
'reconnect to update
Set con = getConn()
Set rs.ActiveConnection = con
rs.UpdateBatch '<<< transfer to DB happens here: no loop!
rs.Close
'requery to demonstrate insert was successful
rs.Open "select * from Table1", con, _
adOpenDynamic, adLockBatchOptimistic
Do While Not rs.EOF
Debug.Print rs("ID").Value, rs("UserName").Value
rs.MoveNext
Loop
rs.Close
con.Close
End Sub
Function getConn() As ADODB.Connection
Dim rv As New ADODB.Connection
Dim strConn As String
strConn = "Provider=Microsoft.ACE.OLEDB.12.0;" _
& "Data Source = " & ThisWorkbook.Path & "\Test.accdb"
rv.Open strConn
Set getConn = rv
End Function
VBA 记录集虚拟存在于运行时调用的内存中,直到它们包含在实际的物理格式中(即 csv、txt、xlsx、xml、数据库临时文件 table)保存到硬盘。这类似于 R 或 Python pandas、SAS 数据集、PHP 数组和其他数据结构中的数据帧。
考虑使用 CopyFromRecordset methods into an Excel spreadsheet to be saved as csv, txt, xlsx, or xml. Alternatively, you can use the Save 方法以这种格式导出 ADO,以将记录集保存为持久格式类型,如 xml。
然后,将生成的文件附加到 MS Access table 及其自动数据迁移功能:
- 对于电子表格:
DoCmd.TransferSpreadsheet
- 对于 txt、csv 或其他带分隔符的文件:
DoCmd.TransferText
- xml 个文件:
Application.ImportXML
- 对于本地或ODBC/OLEDB链接数据库tables:
INSERT INTO
追加SQL查询
要使用 SQL 语句完成此操作,请使用 SELECT/INSERT... IN [Designate DB A; record posted to] or FROM... IN [指定 DB B;记录原始出处]
您只能在单个查询中使用一次 IN 语句。因此,您使用 ADODB 连接创建另一个连接以确定另一个源连接。
Function example()
Dim dB_External As String
Dim db_Local As String
Dim cnLocal As ADODB.Connection
Dim cnExternal As ADODB.Connection
Set cnLocal = CurrentProject.Connection
Set cnExternal = New ADODB.Connection
cnExternal .Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\...accdb;Persist Security Info=False;"
dB_External = "C:\Users\...accdb"
db_LOCAL = "C:\Users\...accdb"
Example A:
strSQL = "INSERT INTO *Local table to receive records* (Column Designations)"
strSQL = strSQL & " SELECT ( *Corresponding records from external table* )"
strSQL = strSQL & " FROM *External table name* IN '" & dB_External & "'"
cnLocal.Execute (strSQL)
我使用上面的代码,如果我 select 从单个外部 table.
使用本地 ADODB 连接Example B:
strSQL = "INSERT INTO *Local table to receive records* (Column Designations) IN '" & dblocal & "'"
strSQL = strSQL & " ( *Corresponding records from external table* )"
strSQL = strSQL & " FROM *External table name*
cnExternal.Execute (strSQL)
我使用上面的代码使用外部 ADODB 连接,如果我 select 涉及在外部数据库中加入多个 table。