Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


How to make multi-query result in Access?

Posted on 2012-08-28
Medium Priority
Last Modified: 2012-09-07
I can't provide a specific query because i'm still working on the formatting so i'll provide a generalization and thank you in advance for your understanding.

I have TWO queries that I need output to Excel.

Query 1 Result View:
     Line     Customer     ProdDesc      TotalAmount
     1          Acme            Widgets        $1,000.00
     2          Beta              Tools            $500.50

Query 2 Result Veiw:

Output to CSV/Excel:
     <Carriage Return>
     Line     Customer     ProdDesc      TotalAmount
     1          Acme            Widgets        $1,000.00
     2          Beta              Tools            $500.50

I assume i simply make Query2 a MAKETABLE query and then Query1 an APPEND to existing CSV file but don't know how to do that or if that's the correct method.

Another option is to create a blank table and append these two queries, plus a third query that will add the words "Line     Customer     ProdDesc      TotalAmount" before appending Query1 and then exporting the table to csv.

but any suggestions would be helpful.

thanks for your help!
Question by:intsup
  • 2
  • 2
LVL 40

Accepted Solution

als315 earned 2000 total points
ID: 38342443
Csv is text file and you can write it with this sub:
Sub write_csv()
Dim fileN As String, FileO As Integer, RC As Long
Dim rst As DAO.Recordset
Dim Qry As String
Qry = "Query1" ' your query
fileN = "c:\tmp\test.csv" ' output file
FileO = FreeFile
RC = DCount("*", Qry)
Set rst = CurrentDb.OpenRecordset(Qry)
Open fileN For Output As #FileO
Print #FileO, "TotalLines="; RC
Print #FileO, "Line"; Tab; "Customer"; Tab; "ProdDesc"; Tab; "TotalAmount"
Do Until rst.EOF
 Print #FileO, rst!Line; Tab; rst!Customer; Tab; rst!ProdDesc; Tab; rst!TotalAmount
Set rst = Nothing
Close #FileO
End Sub

Open in new window

In example csv is tab separated, but you can change Tab to ","

Author Comment

ID: 38352362

if that works then that is exactly what i need.

i am using access 2007, where do i put this sub?  (is it a module?)
how do i call this procedure?
LVL 40

Assisted Solution

als315 earned 2000 total points
ID: 38353046
You can add button to some form and put this code to button's press event.

Author Comment

ID: 38377371
thanks!  i will try it out and hope for the best..

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

571 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