Results 1 to 6 of 6
  1. #1
    Lifeseeker1019 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Mar 2014
    Posts
    6

    Export data into Excel, VBA

    Hello,

    I am trying to export a table of records into Excel. I have some code already but it's only importing the 1st row. Not sure why.

    Code:
    Sub GenerateOutliers_Click()
    
    Dim MyDb As DAO.Database
    Dim rst As DAO.Recordset
    Dim strSQL As String
    
    Dim myExcel As Object
    Dim myWb As Object
    Dim myWs As Object
    Dim rng As Range
    Dim rowsToReturn As Integer
    
    
    Dim iCol As Integer
    
    
    strSQL = "SELECT * FROM tbl_Outliers"
    Set rst = CurrentDb.OpenRecordset(strSQL)
    rowsToReturn = rst.RecordCount
    
    Set myExcel = CreateObject("Excel.Application")
    Set myWb = myExcel.workbooks.Open(fileOutliers)
    myExcel.Visible = True
    Set myWs = myWb.worksheets("Outliers")
    
    
    For iCol = 0 To rst.Fields.Count - 1
    myWs.Cells(1, iCol + 1).Value = rst.Fields(iCol).Name
    Next
    
    Set rng = myWs.Cells(2, 1)
    
    
    rng.CopyFromRecordset rst, rowsToReturn
    
    
    rst.Close
    Set rst = Nothing
    Set myWs = Nothing
    Set myWb = Nothing
    
    
    
    End Sub
    Thank you for your assistance

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    Perhaps:

    http://www.baldyweb.com/RecordCounts.htm

    I would drop the rows to return, I never use it with CopyFromRecordset.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    ranman256's Avatar
    ranman256 is online now VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,521
    No need to cycle thru records.
    use transferspreadsheet. It's instantaneous.
    or
    copyFromRecordset.

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    OP is not cycling through records, they're cycling through fields (to create a header row). They're already using CopyFromRecordset, just need to drop the optional max rows argument:

    The maximum number of records to copy onto the worksheet. If this argument is omitted, all the records in the Recordset object are copied.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    Lifeseeker1019 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Mar 2014
    Posts
    6
    Quote Originally Posted by pbaldy View Post
    OP is not cycling through records, they're cycling through fields (to create a header row). They're already using CopyFromRecordset, just need to drop the optional max rows argument:

    The maximum number of records to copy onto the worksheet. If this argument is omitted, all the records in the Recordset object are copied.
    Got it now! Thank you for explaining

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    Happy to help!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Getting data into excel without export
    By chriswrcg in forum Access
    Replies: 3
    Last Post: 04-09-2020, 10:29 PM
  2. Replies: 26
    Last Post: 04-07-2020, 02:29 AM
  3. Transposing columnar data to row data; Export to excel for SPSS
    By jondavidf in forum Import/Export Data
    Replies: 6
    Last Post: 01-25-2019, 06:42 PM
  4. Export data to excel
    By joanas in forum Access
    Replies: 4
    Last Post: 03-02-2015, 10:44 AM
  5. How to export all data to excel?
    By s128 in forum Import/Export Data
    Replies: 5
    Last Post: 06-17-2013, 01:22 PM

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