Insert Complete RecordSet to another table in other database MS Access

独自空忆成欢 提交于 2019-12-08 05:43:38

问题


I have 2 Tables with same colums in two diferent files. One file "Base PT.accdb" where i want to insert all the Recordset from the other file "Sanchez_Mar.mdb". Both tables have 7 Columns with the same name.

I had a lot of problems using "System.Data.OleDb.OleDbConnection" so i decided to use RecordSets. At the moment i'm using loop to insert all, but there is a lot of data so it take quite long. It's posible to insert all the recordset to the other database ?

this is the part of my code i need help:

Sub Copiar_BasePT2(RutaPT As String)
  Dim Cant as Long
  Dim db As Database
  Dim dbBase_PT As Database
  Set dbBase_PT = OpenDatabase(RutaPT & "\Base PT.accdb")
  Set db = CurrentDb
  Dim rsSYM As Recordset
  Set rsSYM = db.OpenRecordset("SELECT * from FINAL_Alarma_SYM")
  rsSYM.MoveLast
  Cant = rsSYM.RecordCount
  rsSYM.MoveFirst

  For i = 1 To Cant 
          dbBase_PT.Execute ("Insert into Tab_PT values('" & rsSYM.Fields(0) & "','" & rsSYM.Fields(1) & "','" & rsSYM.Fields(2) & "','" & rsSYM.Fields(3) & "','" & rsSYM.Fields(4) & "','" & rsSYM.Fields(5) & "','" & rsSYM.Fields(6) & "');")
          rsSYM.MoveNext

          DoEvents

  Next i
End Sub

It is posible to insert all the recordset ? Or how can I bring the table From "Sanchez_Mar.mdb" to "Base PT.accdb" faster?


回答1:


You can run an insert query using your other database as the source for the data.

"INSERT INTO Tab_PT (Field1, Field2, Field3, Field4, Field5, Field6, Field7) " & _
" SELECT Field1, Field2, Field3, Field4, Field5, Field6, Field7 " & _
" FROM [MS Access;DATABASE=PathToSanchez_MarDB].FINAL_Alarma_SYM"



回答2:


Do the following from the source database:

1) Create a linked table to the destination table.

  • On the External Data tab, in the Import group, click Access.
  • Select Link rather than import
  • Browse to the destination database and select the file
  • Select the destination table from the list

2) Create an append query selecting from the source table and appending to the linked table.

  • Create a select query by selecting each field independently
  • Change the query to an append query by clicking "Append" in the Design tab
  • Select the (linked) destination table

3) Run it

This is probably as fast as it gets if you already have the destination table. If you don't mind starting over, you could delete the destination table and just do an import from the destination database.



来源:https://stackoverflow.com/questions/31249181/insert-complete-recordset-to-another-table-in-other-database-ms-access

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!