Share via

Importing data from SQL Server to Access 2010

Anonymous
2014-05-06T20:54:23+00:00

I am trying to collect specific data from a series of tables in a SQL Server database and combine them together in a single table in Access using VBA.  

I created a recordset in Access using a query that combines all the data using multiple UNION ALL statements.  My initial approach was to iterate through the recordset with something like:

    Dim cnn As New ADODB.Connection

    Dim rs As New ADODB.Recordset

    Dim SQL As String

    cnn.Open "Driver={SQL Server};Server=" + SQLServerNameStr + ";Database=" + DBNameStr + ";Trusted_Connection=Yes"

    SQL = "SELECT A, B, C FROM TableA UNION ALL SELECT D, E, F FROM TableB UNION ALL ..." // etc., etc., etc.

    rs.Open SQL, cnn, adOpenForwardOnly

    While Not rs.EOF

        CurrentDB().Execute ("INSERT INTO AccessTable VALUES ('" & rs("FieldA") & "', '" & rs("FieldB") & "', '" & rs("FieldC") &"')")

        rs.MoveNext

    Wend

This all works, however, it is excruciatingly slow for approximately 20,000 records.  I am sure there is a better way ... probably by building the INSERT into the main query, however, I cannot wrap my head around how to do this when the data source is on a connection to SQL Server and the destination in the current database.

Any suggestions would be greatly appreciated.

Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Duane Hookom 26,825 Reputation points Volunteer Moderator
2014-05-06T21:20:14+00:00

I would create a pass-through query with the union SQL statement. Then create an append query with the p-t as the source.

Was this answer helpful?

0 comments No comments

8 additional answers

Sort by: Most helpful
  1. Tom van Stiphout 40,211 Reputation points MVP Volunteer Moderator
    2014-05-07T13:45:10+00:00

    Dynamically? Sure. You can set the SQL property of any querydef, including a passthrough query.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2014-05-07T13:42:09+00:00

    Thanks for your response, unfortunately I don't think this will work as this process needs to be run on many different SQL databases.  The user will select the server and database to perform the action upon.  The databases are all production databases of the same form, but I believe I would have to set up a pass through query every time this process was to be run, which is not practical.

    Is there is a way to dynamically create the pass through query?

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2014-05-07T13:36:11+00:00

    I had thought of that, but the problem with that idea is that this will need be run on many different production databases.  I don't think that I could convince the SQL Developers to add a View to the production code for thousands of databases that would only be used on a very small percentage of them once or twice by our team.

    Was this answer helpful?

    0 comments No comments
  4. Tom van Stiphout 40,211 Reputation points MVP Volunteer Moderator
    2014-05-07T03:29:52+00:00

    One alternative would be to create a VIEW in SQL Server and link to it from Access. Then your insert statement would be trivial: insert into myAccessTable select * from mySqlView

    Was this answer helpful?

    0 comments No comments