insert full ADO Recordset into existing ACCESS table WITHOUT LOOP

后端 未结 4 1979
再見小時候
再見小時候 2020-12-05 15:55

I have a filled ADO recordset in my VBA module. I also have a table in ACCESS that has exactly the same structure as the recordset.

Now I fill the table using a loo

4条回答
  •  无人及你
    2020-12-05 16:27

    To accomplish this with a SQL statement you use the SELECT/INSERT... IN [Designate DB A; record posted to] or FROM... IN [Designate DB B; record original source]

    You can only use the IN statement once in a single query. Therefore you create the other connection using the ADODB connection to determine the other source connection.

    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)
    

    I use the above code, with the local ADODB connections if I select from a single external table.

    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)
    

    I use the above code using the external ADODB connection, if I select involves joining multiple tables in the external db.

提交回复
热议问题