1

I am using the following code to insert data in an access table from SQL using a recordset. This table is further used for other operations. While this is inserting the data into the table perfectly, the time taken by this huge. Should I use any other method to insert data into the table to reduce the time taken?

Do Until rs.EOF
          DoCmd.RunSQL "INSERT INTO Table (Alpha,Beta,Gamma) VALUES(" & _
          rs.Fields(0).Value & ",'" & rs.Fields(1).Value & "'," & rs.Fields(2).Value _
          & " );"
        rs.MoveNext
Loop
3
  • Can you please elaborate your code Commented Mar 3, 2014 at 13:29
  • Are you inserting identical data into every row? Why do you keep doing it in a loop? Just run the sql once, you are running the same sql for every row in a table. Commented Mar 3, 2014 at 14:16
  • yes I am doing it in a loop. But what would be the best way so that I can insert the data in a single shot. Commented Mar 4, 2014 at 6:37

3 Answers 3

1
  1. Create a linked table to the SQL table, say it's called MyLinkedTable
  2. Create an Append query to append to your local Access table from your linked table. It will look something like this: INSERT INTO MyAccessTable (Field1,Field2...) SELECT Field1,Field2... FROM MyLinkedTable;
Sign up to request clarification or add additional context in comments.

Comments

0

If you could select the data in the SQL UPDATE statement instead looping in VBA it would take a fraction of the time as all the work would be done by the server side.

7 Comments

is it possible to do that is vba. My table is in access. This table is used for other operations.
The whole idea is that don't want to do this in VBA because it takes too long. You want do it all in SQL. Tell me, is the source table in the same database as the destination table?
Source database is in SQL but destination table is in access.
Do you mean SQL Server? How do you connect to the source table?
Yes SQL Server. Apologies for confusion. I am connecting to SQL Server using ADODB.
|
0

You just need to do an INSERT INTO SELECT. We need more information on your RecordSet but you probably do not need it.

'Here we go, with just one line! DoCmd.RunSQL "INSERT INTO Table (Alpha,Beta,Gamma) SELECT column1, column2, column2 FROM YourTable" The SELECT statement is probably the same as the one you used for opening your Recordset. Good luck!

1 Comment

This will only work if "YourTable" is a linked table to the SQL Server.

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.