Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Insert ADO recordset to table

Posted on 2014-03-11
10
Medium Priority
?
4,139 Views
Last Modified: 2014-03-13
Hi,

I have a VBA that queries a database and gets a set of data into a recordset.  I need a way to copy this data into the table.   The recordset and the table both have a same structure (1 column) and the recordset is usually large (100,000 rows) and the table is cleared before it is filed with the data.

Any way I can do an insert that copies whole recordset into table?

I've seen lots of examples where we go through each recordset and do an insert, but due to the size, i can't imagine this method being efficient.
0
Comment
Question by:iamnamja
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
10 Comments
 
LVL 58
ID: 39920752
<<Any way I can do an insert that copies whole recordset into table? >>

  In short, no.    But what you can do is execute an INSERT with a SELECT based on the one you just used to get the record set.

Jim.
0
 

Author Comment

by:iamnamja
ID: 39920782
Hi Jim,

Could you give me an example of how this would work?


Here's my current code inserting line by line..

all_SQL = "SELECT distinct i.sid " & _
            "FROM common..idlist i"


Set all_RS = query_DB(globconn, all_SQL)

DoCmd.SetWarnings False
DoCmd.RunSQL "DELETE * FROM Latest_TIDs"
DoCmd.SetWarnings True

If all_RS.State <> 0 Then
    If Not (all_RS.EOF And all_RS.BOF) Then
        all_RS.MoveFirst
        DoCmd.SetWarnings False
        Do While Not all_RS.EOF
            DoCmd.RunSQL "INSERT INTO Latest_SIDs (sid) VALUES(""" & all_RS!sid.Value & """)"
            all_RS.MoveNext
        Loop
        DoCmd.SetWarnings True
    End If
End If

Open in new window

0
 
LVL 58
ID: 39920882
all_SQL = "INSERT INTO Latest_SIDs ( SID ) SELECT distinct i.sid FROM common..idlist i"

CurrentDB.Execute all_SQL, dbFailOnError

This assumes the tables are available as linked tables as with CurrentDB().Execute or DoCmd.RunSQL, you are not using ADO to execute the query (linked tables use ODBC).

Jim.
0
TCP/IP Network Protocol Cheat Sheet

TCP/IP is a set of network protocols which is best known for connecting the machines that make up the Internet. The truth is that TCP/IP is one of the oldest network protocols and its survival is mainly based on its simplicity and universality.

 

Author Comment

by:iamnamja
ID: 39920888
Hi Jim,

Unfortunately the tables are NOT linked - actually can't be linked, so i would need to query it the way I did above.  Does that mean the only method is the way that I have currently written?
0
 
LVL 58
ID: 39921082
<<Unfortunately the tables are NOT linked ->>

 I don't understand how your RunSQL works then.

Jim.
0
 
LVL 58
ID: 39921096
<<Does that mean the only method is the way that I have currently written? >>

 missed this...no, you can execute the SQL via ADO:

<connection object>.Execute all_SQL

Jim.
0
 

Author Comment

by:iamnamja
ID: 39921240
So just so that we're on the same page... The all_rs query is run on a sybase database and the recordset data fetched is what i need to store into my table called Latest_SIDs.

How do i run a query that inserts a data from a sybase database into access?
0
 
LVL 58

Accepted Solution

by:
Jim Dettman (Microsoft MVP/ EE MVE) earned 2000 total points
ID: 39921632
ah, so Latest_SIDs  is local...now I understand the problem.

In that case, you will need to use the IN clause on the select statement to specify the external database.   With that, you must call an ODBC driver.   Basically, your doing a linked table on the fly.  Will look something like this:

all_SQL = "INSERT INTO Latest_SIDs ( SID ) SELECT distinct i.sid FROM common..idlist IN '' [ODBC; Driver={MySQL ODBC 5.2 ANSI Driver};Server=localhost;Database=myDataBase;
User=myUsername;Password=myPassword;Option=3;"

CurrentDB.Execute all_SQL, dbFailOnError

Your connection info will vary of course, but that's the jist of it.   You probably can do this with ADO as well, but off-hand I don't know how.

Jim.
0
 
LVL 39

Expert Comment

by:PatHartman
ID: 39921754
I don't understand why you can't link the table.  The link doesn't need to be permanent.  You can use TransferDatabase to link and then delete the link when you are done.
0
 
LVL 58
ID: 39921957
A linked table is faster performance wise too...

Jim.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

660 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question