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
Solved

Code to output query to Excel

Posted on 2011-02-14
8
559 Views
Last Modified: 2012-05-11
This question is further to a previous question I asked and for which a solution was provided by capricorn:

http://www.experts-exchange.com/Microsoft/Development/MS_Access/Q_26820656.html

In the first part of my question (link above) I run a query and Cap has provided code which writes the query output to Sheet1 of an Excel workbook.  I now want to run a second query and would like to output the query result to Excel starting in the row just after the last row (record) of the first query output.  Thanks.
0
Comment
Question by:RishiSingh05
  • 4
  • 4
8 Comments
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 34891859

    Dim rs As DAO.Recordset
Dim xlObj As Object, Sheet As Object, iCol As Integer
Dim lastRow as long

    Set xlObj = CreateObject("Excel.Application")
    xlObj.Workbooks.Add
    xlObj.Visible = True
    Set rs = CurrentDb.OpenRecordset("m1Revenue1")
    Set Sheet = xlObj.activeworkbook.workSheets("Sheet1")
        For iCol = 0 To rs.Fields.Count - 1
            Sheet.cells(1, iCol + 1).Value = rs.Fields(iCol).Name
        Next
    Sheet.Range("A2").CopyFromRecordset rs  'copy the data
   
lastRow=Sheet.usedrange.rows.count

     Set rs = CurrentDb.OpenRecordset("query2")
         For iCol = 0 To rs.Fields.Count - 1
            Sheet.cells(lastRow+1, iCol + 1).Value = rs.Fields(iCol).Name
        Next
    Sheet.Range("A" & lastRow +2).CopyFromRecordset rs  'copy the data    




0
 

Author Comment

by:RishiSingh05
ID: 34891943
Here is the code behind button2 which runs query2 ...

Which part of your provided code above do I need to use?  I don't want to re-run the query1 (m1Revenue1) as that has been run already and copied to Excel already.  Excel is still open as I run query2.

Private Sub Cmd5_Q5_Click()
On Error GoTo Err_Cmd5_Q5_Click

    Dim stDocName As String

    stDocName = "m1Revenue2_falloff"
    DoCmd.OpenQuery stDocName, acNormal, acEdit
    
Exit_Cmd5_Q5_Click:
    Exit Sub

Err_Cmd5_Q5_Click:
    MsgBox Err.Description
    Resume Exit_Cmd5_Q5_Click
    
End Sub

Open in new window

0
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 500 total points
ID: 34891964
just run it one time...

    Set xlObj = CreateObject("Excel.Application")
    xlObj.Workbooks.Add
    xlObj.Visible = True
    Set rs = CurrentDb.OpenRecordset("m1Revenue1")
    Set Sheet = xlObj.activeworkbook.workSheets("Sheet1")
        For iCol = 0 To rs.Fields.Count - 1
            Sheet.cells(1, iCol + 1).Value = rs.Fields(iCol).Name
        Next
    Sheet.Range("A2").CopyFromRecordset rs  'copy the data
   
lastRow=Sheet.usedrange.rows.count

     Set rs = CurrentDb.OpenRecordset("m1Revenue2_falloff")
         For iCol = 0 To rs.Fields.Count - 1
            Sheet.cells(lastRow+1, iCol + 1).Value = rs.Fields(iCol).Name
        Next
    Sheet.Range("A" & lastRow +2).CopyFromRecordset rs  'copy the data    


0
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 34891968
and you don't need to open the query.
0
 

Author Comment

by:RishiSingh05
ID: 34892042
ok now I understand  .... thanks.
0
 

Author Comment

by:RishiSingh05
ID: 34897242
I am trying to add a third query but I am not getting it to work.  Pls see code below.
Private Sub Cmd_RunSelectQueries_Click()


    Set xlObj = CreateObject("Excel.Application")
    xlObj.Workbooks.Add
    xlObj.Visible = True
    Set rs = CurrentDb.OpenRecordset("m1Revenue1")
    Set Sheet = xlObj.activeworkbook.workSheets("Sheet1")
        For iCol = 0 To rs.Fields.Count - 1
            Sheet.cells(1, iCol + 1).Value = rs.Fields(iCol).Name
        Next
    Sheet.Range("A2").CopyFromRecordset rs  'copy the data
   
lastRow = Sheet.usedrange.rows.Count

     Set rs = CurrentDb.OpenRecordset("m1Revenue2_falloff")
         For iCol = 0 To rs.Fields.Count - 1
            Sheet.cells(lastRow + 1, iCol + 1).Value = rs.Fields(iCol).Name
        Next
    Sheet.Range("A" & lastRow + 2).CopyFromRecordset rs 'copy the data


     Set rs = CurrentDb.OpenRecordset("Members1")
         For iCol = 0 To rs.Fields.Count - 1
            Sheet.cells(lastRow + 1, iCol + 1).Value = rs.Fields(iCol).Name
        Next
    Sheet.Range("A" & lastRow + 2).CopyFromRecordset rs 'copy the data
  
End Sub

Open in new window

0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 34897773
you need to find the lastRow again



Private Sub Cmd_RunSelectQueries_Click()


    Set xlObj = CreateObject("Excel.Application")
    xlObj.Workbooks.Add
    xlObj.Visible = True
    Set rs = CurrentDb.OpenRecordset("m1Revenue1")
    Set Sheet = xlObj.activeworkbook.workSheets("Sheet1")
        For iCol = 0 To rs.Fields.Count - 1
            Sheet.cells(1, iCol + 1).Value = rs.Fields(iCol).Name
        Next
    Sheet.Range("A2").CopyFromRecordset rs  'copy the data
   
lastRow = Sheet.usedrange.rows.Count

     Set rs = CurrentDb.OpenRecordset("m1Revenue2_falloff")
         For iCol = 0 To rs.Fields.Count - 1
            Sheet.cells(lastRow + 1, iCol + 1).Value = rs.Fields(iCol).Name
        Next
    Sheet.Range("A" & lastRow + 2).CopyFromRecordset rs 'copy the data

lastRow = Sheet.usedrange.rows.Count  ' <<< ADD HERE
     Set rs = CurrentDb.OpenRecordset("Members1")
         For iCol = 0 To rs.Fields.Count - 1
            Sheet.cells(lastRow + 1, iCol + 1).Value = rs.Fields(iCol).Name
        Next
    Sheet.Range("A" & lastRow + 2).CopyFromRecordset rs 'copy the data
  
End Sub

Open in new window

0
 

Author Comment

by:RishiSingh05
ID: 34906661
Thanks.  I was also able to figure it out.  Sorry for the extra work !!
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

809 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