0

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.

3
  • 1
    Your VBA code for your execute statement is incorrect, how does this even run? Commented May 6, 2014 at 19:17
  • I was trying to simplify ... I didn't actaully cut and paste my code ... I was just looking at how many mistakes I make when trying to code without an editor correcting my syntax!!! ;) Commented May 6, 2014 at 19:22
  • I have edited the question to correct the syntax. Commented May 6, 2014 at 19:35

3 Answers 3

1

Try using DoCmd.TransferDatabase* to transfer data from a SQL Server view, that queries the data the way you need it, into an Access table.

DoCmd.TransferDatabase acImport, "ODBC Database", _ 
"Driver={SQL Server};Server=" & SQLServerNameStr & ";Database=" & DBNameStr & _
";Trusted_Connection=Yes", acTable, "SomeSqlServerView", "AccessTable"

Hopefully the transfer database method does a good job of managing insertions and speeds up the import process.


*Changed my answer since batch updates don't work with Access 2010 as a data destination.

Sign up to request clarification or add additional context in comments.

2 Comments

dbOptimisitcBatch / dbUpdateBatch are only supported for ODBCDirect workspaces ... can I open an native Access table in this way?
Ah, you're right, batch updates do not seem to be supported. Now that I'm thinking about it I was writing to SQL Server from a VB6 application.
0

I haven't used native tables so much, but I suspect the issue is related to inserting one at a time.

You might want to try doing the insert through SQL and doing 50 rows at a time or so.

Comments

0

Why not try doing an Insert query instead creating a recordset? See this question: How to insert "Entire" DAO recordset into a table with VBA

4 Comments

This is what I was hoping to do, but I cannot figure out how to wrap the SELECT query, which is on a SQL Server connection, inside the INSERT query that is on the CurrentDb()
INSERT INTO AccessTable (FieldA, FieldB, FieldC) SELECT (FieldA, FieldB, FieldC) FROM (SELECT A, B, C FROM TableA UNION ALL SELECT D, E, F FROM TableB UNION ALL ..." // etc., etc., etc.)
Maybe I am being think (it wouldn't be the first time) but I am going to run the query by calling CurrentDB().Execute so the INSERT will go into a table in the Access database, but how do I then tell the query that the SELECT statement is coming from the SQL Server using the ADODB (or some other type) connection, not the currentdb?
One way to do it would be to import all the source tables into your Access DB as linked tables, or even copy all the data across.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.