Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Export Multiple Queries to excel and append to one sheet

Posted on 2011-09-07
17
Medium Priority
?
487 Views
Last Modified: 2012-05-12
access 2003
excel 2003
vba function needed
workbook name :  C:\Program Files\MyWork\MyResults.xls
I have about 15 queries to append to a worksheet("ResultData")

All with various Names:
Export Status Codes
Export Red Codes
Export Resgualr Codes
etc..

But these names I could put in a text file
to loop through?

I need a VBA routine to append each query and its field headers to append to each other.


Thanks
fordraiders
0
Comment
Question by:fordraiders
[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
  • 9
  • 8
17 Comments
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 36500056
are the queries returning different number of columns?


you can start with these codes


Dim db As DAO.Database, rs As DAO.Recordset
Dim i As Integer, j As Integer
Dim ssql As String, xlFile As String
Dim xlObj As Object
Dim Sheet As Object, rowCnt as integer

Set db = DBEngine.Workspaces(0).Databases(0)

Set xlObj = CreateObject("Excel.Application")
        xlObj.workbooks.Open ("C:\Program Files\MyWork\MyResults.xls")
        Set rs = db.OpenRecordset("Export Status Codes")
    
Set Sheet = xlObj.ActiveWorkbook.Sheets("ResultData")
xlObj.Visible = True
'copy the headers
Dim iRow, iCol
iRow = 1
    For iCol = 0 To rs.Fields.Count - 1
        Sheet.cells(iRow, iCol + 1).Value = rs.Fields(iCol).Name
    Next

Sheet.range("A2").CopyFromRecordset rs  'this copy just the data



'check how many rows where  populated
rowCnt=Sheet.usedrange.rows.count
 
 Set rs = db.OpenRecordset("Export Red Codes")

iRow = rowCnt + 1
 
    For iCol = 0 To rs.Fields.Count - 1
        Sheet.cells(iRow, iCol + 1).Value = rs.Fields(iCol).Name
    Next

Sheet.range("A" & iRow + 1).CopyFromRecordset rs  'this copy just the data

'and so on



rs.Close
Set Sheet = Nothing
Set xlObj = Nothing

Open in new window





* Note : you can place the names of the queries in an Array or in a table



0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 36500453
* save the name of the queries in a table "tblQueries" with Field Name "QueryName"
 use this code

Dim db As DAO.Database, rs As DAO.Recordset, rsQ As DAO.Recordset
Dim strQry As String
Dim xlObj As Object
Dim Sheet As Object, rowCnt As Integer
Dim iRow, iCol

Set db = DBEngine.Workspaces(0).Databases(0)

Set xlObj = CreateObject("Excel.Application")
    xlObj.Workbooks.Open ("C:\Program Files\MyWork\MyResults.xls")
    
    Set Sheet = xlObj.ActiveWorkbook.Sheets("ResultData")
    rowCnt = 0
    
    
    Set rsQ = db.OpenRecordset("tblQueries")
    rsQ.MoveFirst
    
    Do Until rsQ.EOF
        iRow = rowCnt + 1
        strQry = rsQ!queryName
        
        Set rs = db.OpenRecordset(strQry)
 
 'copy the headers
        
            For iCol = 0 To rs.Fields.Count - 1
                Sheet.cells(iRow, iCol + 1).Value = rs.Fields(iCol).Name
            Next
  'this copy just the data
            Sheet.range("A" & iRow + 1).CopyFromRecordset rs

'check how many rows where  populated
            rowCnt = Sheet.usedrange.rows.Count
        
        rsQ.MoveNext
    Loop

'save the file
    xlObj.ActiveWorkbook.Save
    
rsQ.Close
rs.Close
Set Sheet = Nothing
xlObj.Quit
Set xlObj = Nothing

Open in new window





0
 
LVL 3

Author Comment

by:fordraiders
ID: 36500621
no, its the same fields
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 36502068
you can use a union query then export the result, but i'll prefer using the vba codes posted at http:#a36500453


did you try it?
0
 
LVL 3

Author Comment

by:fordraiders
ID: 36502586
gonna try now...Thanks the second piece of code http:#a36500453
0
 
LVL 3

Author Comment

by:fordraiders
ID: 36502701
cap, Error here

Do Until rsQ.EOF
        iRow = rowCnt + 1
        strQry = rsQ!queryName
       
        Set rs = db.OpenRecordset(strQry)  <--- name not available  error 3265  Item not found in this collection.
         I have 4 querynames in the table  
but   strQry = ""
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 36502709
what is the name of the field in your table "tblQueries"
0
 
LVL 3

Author Comment

by:fordraiders
ID: 36502812
"UnsolicatedQueries"
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 36502874
can you upload a copy of the db  (.mdb version )
0
 
LVL 3

Author Comment

by:fordraiders
ID: 36502884
it has links to network tables..?
0
 
LVL 3

Author Comment

by:fordraiders
ID: 36502897
hold on a sec...
0
 
LVL 3

Author Comment

by:fordraiders
ID: 36502922
its 64mb zipped...but I could delete the data ?
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 36502979
open the tblQueries, copy the contents by selecting all of columns and fields and post it here..
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 36502988
did you do a compact and repair before zipping?

yes you can delete some the data, but not the records from tblQueries
0
 
LVL 3

Author Comment

by:fordraiders
ID: 36503206
ok.. I got your first bit of code to work fine...
here is the database...

testing-Prmdb025-chgo-Reporting-.zip
0
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 2000 total points
ID: 36503300
change this part

        strQry = rsQ!queryName

with

        strQry = rsQ!UnsolicatedQueries


here is the whole code
Public Sub LoadCodesUnsolicated()
' ok this is for
Dim db As DAO.Database, rs As DAO.Recordset, rsQ As DAO.Recordset
Dim strQry As String
Dim xlObj As Object
Dim Sheet As Object, rowCnt As Integer
Dim iRow, iCol

Set db = DBEngine.Workspaces(0).Databases(0)

Set xlObj = CreateObject("Excel.Application")
    xlObj.Workbooks.Open ("C:\KidmanUnsolicatedReports\Kidman_Unsolicated_Report.xls")
    
    Set Sheet = xlObj.ActiveWorkbook.Sheets("UnSolicated_Projects")
    rowCnt = 0
    
    
    Set rsQ = db.OpenRecordset("tblUnsolicatedQueries")
    rsQ.MoveFirst
    
    Do Until rsQ.EOF
        iRow = rowCnt + 1
        strQry = rsQ!UnsolicatedQueries
        
        Set rs = db.OpenRecordset(strQry)
 
 'copy the headers
        
            For iCol = 0 To rs.Fields.Count - 1
                Sheet.cells(iRow, iCol + 1).Value = rs.Fields(iCol).Name
            Next
  'this copy just the data
            Sheet.range("A" & iRow + 1).CopyFromRecordset rs

'check how many rows where  populated
            rowCnt = Sheet.usedrange.rows.Count
        
        rsQ.MoveNext
    Loop

'save the file
    xlObj.ActiveWorkbook.Save
    
rsQ.Close
rs.Close
Set Sheet = Nothing
xlObj.Quit
Set xlObj = Nothing

End Sub

Open in new window



0
 
LVL 3

Author Closing Comment

by:fordraiders
ID: 36503491
Thanks as always ! very much !
Great
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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 …

636 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