Solved

Code to output query to Excel

Posted on 2011-02-14
8
566 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
[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
  • 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
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
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…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

733 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