Solved

Code to output query to Excel

Posted on 2011-02-14
8
553 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

773 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