473,395 Members | 1,583 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How do I export multiple queries ?

I have codes for export that looks like this

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdExport_Click()
  2.  
  3. On Error GoTo Err_cmdTest_Click
  4. 'Must 1st set a Reference to the Microsoft Office XX.X Object Library
  5. Dim dlgOpen As FileDialog
  6. Dim strExportPath As String
  7. Const conOBJECT_TO_EXPORT As String = "qryExportMetrics"
  8.  
  9. Set dlgOpen = Application.FileDialog(msoFileDialogFolderPicker)
  10.  
  11. With dlgOpen
  12.   .ButtonName = "Export To"
  13.   .InitialView = msoFileDialogViewLargeIcons
  14.   .InitialFileName = CurrentProject.Path
  15.      If .Show = -1 Then
  16.        'Allow for Root Directory selection: C:\, D:\, etc.
  17.        strExportPath = Replace(.SelectedItems(1) & "\", "\\", "\")
  18.  
  19.        Call DoCmd.TransferSpreadsheet(TransferType:=acExport, _
  20.                                TableName:=conOBJECT_TO_EXPORT, _
  21.                                FileName:=strExportPath & conOBJECT_TO_EXPORT & ".xls")
  22.  
  23.  
  24.  
  25.  
  26.        MsgBox "[" & conOBJECT_TO_EXPORT & "] has been Exported to " & strExportPath & _
  27.                conOBJECT_TO_EXPORT & ".xls", vbInformation, "Export Complete"
  28.      End If
  29. End With
  30.  
  31. 'Set the Object Variable to Nothing.
  32. Set dlgOpen = Nothing
  33.  
  34. Exit_cmdTest_Click:
  35.   Exit Sub
  36.  
  37. Err_cmdTest_Click:
  38.   MsgBox Err.Description, vbExclamation, "Error in cmdTest_Click()"
  39.     Resume Exit_cmdTest_Click
  40.  
  41.  
  42. End Sub
  43.  
  44.  
and This above code export were only able to export one single query/table.

I would like to export more queries to one single excel when I am exporting.

What should I add to my codes to make that happen?
Sep 2 '11 #1

✓ answered by NeoPa

Close.

What you need is to add the three commands (after line #21 of your original code), but they all need the FileName parameter set too :
Expand|Select|Wrap|Line Numbers
  1. Call DoCmd.TransferSpreadsheet(TransferType:=acExport, _
  2.                                TableName:="qry3", _
  3.                                FileName:=strExportPath & conOBJECT_TO_EXPORT & ".xls")
  4. Call DoCmd.TransferSpreadsheet(TransferType:=acExport, _
  5.                                TableName:="qry4", _
  6.                                FileName:=strExportPath & conOBJECT_TO_EXPORT & ".xls")
  7. Call DoCmd.TransferSpreadsheet(TransferType:=acExport, _
  8.                                TableName:="qryCapacity", _
  9.                                FileName:=strExportPath & conOBJECT_TO_EXPORT & ".xls")

7 4062
NeoPa
32,556 Expert Mod 16PB
When exporting multiple tables/queries to a single workbook it is necessary to execute the TransferSpreadsheet command multiple times with the same value of FileName. The Tablename value would change each time, and this value is also used to name the resultant Worksheet within the Workbook.

I have no knowledge of which other queries you intend to include, nor how you intend to determine these, so I can't help further at this stage. If you want to add that information into the thread we may be in a better position to help.
Sep 2 '11 #2
I have three other queries that I would like to be exported as well and they are:

qryCapacity
qry3
qry4

and I would like the three queries to be exported all together so that when it has exported, everything will be in one single excel file but in all separate worksheet.

I had someone helped me before in one of my threads. He was successful in helping me export all my queries at one time but he set it in a way that there is no option for me to select where I would like to my file to i.e on a desktop, c drive etc.

With the above code, I was able to export only one single query and I have an option as to where to save my exported file to.


Do I add something like this to my existing codes?

Expand|Select|Wrap|Line Numbers
  1. Call DoCmd.TransferSpreadsheet(TransferType:=acExport, _
  2.                                TableName:="qry3")
  3. Call DoCmd.TransferSpreadsheet(TransferType:=acExport, _
  4.                                TableName:="qry4")
  5. Call DoCmd.TransferSpreadsheet(TransferType:=acExport, _
  6.                                TableName:="qryCapacity")
  7.  
  8.  
Sep 2 '11 #3
NeoPa
32,556 Expert Mod 16PB
Close.

What you need is to add the three commands (after line #21 of your original code), but they all need the FileName parameter set too :
Expand|Select|Wrap|Line Numbers
  1. Call DoCmd.TransferSpreadsheet(TransferType:=acExport, _
  2.                                TableName:="qry3", _
  3.                                FileName:=strExportPath & conOBJECT_TO_EXPORT & ".xls")
  4. Call DoCmd.TransferSpreadsheet(TransferType:=acExport, _
  5.                                TableName:="qry4", _
  6.                                FileName:=strExportPath & conOBJECT_TO_EXPORT & ".xls")
  7. Call DoCmd.TransferSpreadsheet(TransferType:=acExport, _
  8.                                TableName:="qryCapacity", _
  9.                                FileName:=strExportPath & conOBJECT_TO_EXPORT & ".xls")
Sep 2 '11 #4
Great stuff NeoPa! You did it again!!!
Sep 2 '11 #5
NeoPa
32,556 Expert Mod 16PB
I try :-)
Sep 2 '11 #6
Now that I got this up and running, I have more questions just for future use.
I will post that question to new thread and I hope you will be able to help me again :DDD
Sep 2 '11 #7
NeoPa
32,556 Expert Mod 16PB
As far as I am able certainly :-)
Sep 3 '11 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Gord | last post by:
I would like to create a summary report from the results of 11 queries (based on 2 tables). All the queries have the same format and return 3 numbers (Count, Current Year Bal, Last Year Bal.)...
2
by: JMCN | last post by:
is it possible to export multiple queries into one workbook, having each query as separate worksheet? i cannot specify a range because the records will change on a daily basis as for query size. i...
4
by: Dave Edwards | last post by:
I understand that I can fill a datagrid with multiple queries, but I cannot figure out how to fill a dataset with the same query but run against multiple SQL servers, the query , table structure...
1
by: mattcatmattcat | last post by:
I have a VB7 aspx file I am creating that requires multiple queries each dependant on the previous queries results. If I run these queries in foxpro, I just run a query then create a cursor with...
8
by: beretta819 | last post by:
Ok, so I apologize in advance for the wordiness of what follows... (I am not looking for someone to make this for me, but to point me in the right direction for the steps I need to take.) I was...
7
by: vaiism | last post by:
I am creating a report that outputs the contact information and details about a water treatment plant, and needs to include information about people who work there. If I tie all the information...
21
by: Kit K | last post by:
I'm new here and will admit right off the bat, most of this is above my head, but I like Access and learning the ticks of the trade so to speak. Here is one and I’m sure there is a way, but I’m...
4
by: Akhenaten | last post by:
I am currently using enterprise manager to run multiple queries on a single table in a DB. I refresh these queries every few minutes. Due to the huge number of them I was looking for a better way...
4
by: forrestgump | last post by:
I want to be able to export multiple access query results to 1 excel worksheet so the results appear under each other. I need this to show the results of headcount, leavers and starters in a cost...
0
by: Emily F | last post by:
In the Macro section of access there is "OutputTo" action which will export a query to it's own spreadsheet in excel -What I need, is a macro (if it exists-but I think it will have to be created...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.