Solved

Code to output query to Excel

Posted on 2011-02-14
8
536 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 119

Expert Comment

by:Rey Obrero
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 119

Accepted Solution

by:
Rey Obrero 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
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 34891968
and you don't need to open the query.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

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 119

Expert Comment

by:Rey Obrero
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now