• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 944
  • Last Modified:

Copy data MSAccess to SQL Server w/VBA & ADO ( bulk Copy)

have an MSAccess database I need to recreate one of it's tables on SQL Server. I need to do it in VBA just after I create the MSAccess Table. What would the syntax be to copy and entire recordset to SQL Server. I know how to do it in a record cursor mode how do you do a bulk copy or can you.
My current code looks something like this:

 Dim mxdb As Database
  Dim MainSet As Recordset

    Set MainSet = mxdb.OpenRecordset( _
    "SELECT D.field1, D.field2, C.field3, C.Field4 " & _
    "FROM (Cubes AS G LEFT JOIN Desc AS C ON G.CLASS = C.Class) " & _
    "     LEFT JOIN Comm AS D ON G.CODE = D.Alpha " & _
    "WHERE G.NAME='UXXX' " & _
    "AND G.CODE = '6' ")
   Set XY_Conn = New ADODB.Connection
   XY_Conn.Provider = "MSDASQL"
   XY_Conn.ConnectionString = "UID=XYadmin;PWD=XY001"
   XY_Conn.Open DSNXX
   Set XY_RecSet = New ADODB.Recordset
   strSQL = "Truncate Table XY_PSA"
   Set XY_RecSet = New ADODB.Recordset
   XY_RecSet.Open strSQL, XY_Conn

   Set XY_RecSet = New ADODB.Recordset
   XY_RecSet.Open "XY_PSA", XY_Conn, adOpenDynamic, adLockOptimistic
   With MainSet
      Do Until MainSet.EOF
         XY_RecSet!field1 = MainSet!field1
         XY_RecSet!field2 = MainSet!field2
         XY_RecSet!field3 = MainSet!field3
         XY_RecSet!field4 = MainSet!field4
   End With

What would I do to reproduce this without walking through each record .
1 Solution
Create a Link to the SQL Server table, and then you can write a Append query that selects data from your access table and inserts into SQL linked table
mbmalmAuthor Commented:
Excellent suggestion. Seems like such a logical solution, but I didn't think of it.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now