Results 1 to 2 of 2
  1. #1
    SteveApa is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2015
    Posts
    84

    create local access table from ADODB sqloledb recordset

    Hi All.
    I have searched, but have not found what I am looking for. I am experienced with VB and DAO - now I just started with ADODB. Our IT dept created an excel file for us to use to get data from a server database. They set it up with a button to refresh data from a server database. I want to connect to the database directly from access and skip their excel file. I used their excel file to give me the connection info (and this forum with google).

    So, I have made the connection to the database and have a recordset opened. How do I put the data from the recordset into a local access table??

    The table has qty 460 fields and 5030 rows. I know access will only let me create a table with max 255 fields - or - I just pick the ones I want with sql

    Code:
    Public Sub test()Dim i As Integer
    Dim sqltxt As String
    Dim rs As ADODB.Recordset
    Dim cn As New ADODB.Connection
    
    
        cn.Provider = "sqloledb.1"
        cn.Properties("Data Source").Value = "xxxxxx.com" ' I hid the real address
        cn.Properties("Initial Catalog").Value = "IDB"
        cn.Properties("Integrated Security").Value = "SSPI"
        cn.Open
        sqltxt = "select [Project Number] from Raw_Data_SV" ' this gets individual fields vs select * from Raw_Data_SV would get me all qty 460 fields
        Set rs = cn.Execute(sqltxt)
    
    
    ' the recordset is now open - I have verified values in the immediate window.  How do I write it to a local access table?
    
    
        rs.MoveFirst
        i = 0
        While Not rs.EOF ' I just did this loop to look thru the recordset
            i = i + 1
            rs.MoveNext
    
    
        Wend
        cn.Close
    
    
    End Sub
    Thanks!
    Steve

  2. #2
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,426
    something like

    Code:
    While Not rs.EOF ' I just did this loop to look thru the recordset
                currentdb.execute("INSERT INTO localtbl (fld1, fld2) VALUES(" & rs!fld1 & ", " & rs!fld2 & ")")
            rs.MoveNext
    Wend

    change names to suit table and field names - and modify the values section to include single quotes for text fields and # for date fields. If date fields are in the format dd/mm/yyyy they need to be formatted US style as mm/dd/yyyy.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 3
    Last Post: 04-07-2016, 04:36 AM
  2. Replies: 8
    Last Post: 01-29-2016, 03:16 PM
  3. How to Create local copy of linked Table
    By behnam in forum Programming
    Replies: 3
    Last Post: 11-20-2014, 05:49 PM
  4. Replies: 4
    Last Post: 08-23-2012, 12:28 AM
  5. Replies: 2
    Last Post: 03-08-2012, 12:59 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums