473,503 Members | 1,710 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Export 1 Query Using Criteria to Multiple Excel Worksheets

habby0123
3 New Member
I am trying to export data to Excel from a query in Access. There is a unique "Sales Region ID" and various "Territory#'s" for each Region. I would like one excel workbook for each region, and a worksheet for each Territory # assigned to that region. I do something similiar with a report that I programmatically convert to a pdf and it works fine, but I'm having a little trouble with the excel export. Can someone give me some advice? (Below is my code at it's current state.) Thanks.

Expand|Select|Wrap|Line Numbers
  1. ------------------------------------------------------------------------------------------------------------------
  2. Function GenerateMedicareFacilityByRegionExcelReport()
  3.  
  4. Dim rsRegion As DAO.Recordset
  5. Dim rsTerritory_Number As DAO.Recordset
  6. Dim qd As DAO.QueryDef
  7.  
  8. 'Create a dummy query for dynamic use
  9. Set qd = CurrentDb.QueryDefs("qryDummy")
  10.  
  11. 'Get the Region
  12. Set rsRegion = CurrentDb.OpenRecordset("Select Distinct [Sales Region ID] From tblSalesRegions")
  13.  
  14. If rsRegion.EOF And rsRegion.BOF Then
  15.   'No Regions found action
  16.   Exit Function
  17. End If
  18.  
  19. 'For each Region we loop for the Territories:
  20. While Not rsRegion.EOF
  21.  
  22.    Set rsTerritory_Number = CurrentDb.OpenRecordset("Select Distinct [Territory#] From tblTerritories Where [Sales Region ID]=" & rsRegion![Sales Region ID])
  23.  
  24.    If rsTerritory_Number.EOF And rsTerritory_Number.BOF Then
  25.         'no Territory# found action = continue
  26.    End If
  27.  
  28.    While Not rsTerritory_Number.EOF
  29.         qd.SQL = "Select * From qryMedicareUnits_by_Facility_Patient_And_Region Where [Sales Region ID]=" & rsRegion![Sales Region ID] & " AND Territory#=" & rsTerritory_Number![Territory#]
  30.         'Here the filled query is used for writing to an excel worksheet named to Region and Territory#
  31.         DoCmd.TransferSpreadsheet 1, 8, "qryDummy", "C:\" & rsRegion![Sales Region ID] & "-" & rsTerritory_Number!Territory# & ".xls", True
  32.  
  33.      rsTerritory_Number.MoveNext
  34.  
  35.   Wend
  36.  
  37.      rsRegion.MoveNext
  38.  
  39. Wend
  40.  
  41. Set rsRegion = Nothing
  42. Set rsTerritory_Number = Nothing
  43.  
  44. End Function
Oct 24 '08 #1
3 5532
NeoPa
32,557 Recognized Expert Moderator MVP
I believe the TableName parameter used in TransferSpreadsheet is used to specify the worksheet name.

You need to take the Territory number OUT of the filename, and rename qryDummy to something representing Territory# for each iteration through the loop.

I would consider making a copy of qryDummy to start with though. This can be deleted at the end of the process. Otherwise, if it is ever cancelled or interrupted then qryDummy may not be available next time (It may be there but with a Territory# for the name).
Oct 25 '08 #2
NeoPa
32,557 Recognized Expert Moderator MVP
A (sort of) continuation of this question can be found at Export multiple queries to multiple sheets in excel.

Probably best continued in here on balance, but for a similar problem, please see the linked thread.
Oct 28 '08 #3
NeoPa
32,557 Recognized Expert Moderator MVP
Heather,

From reading your latest post (in the other thread) in full I get the impression that you're asking for exactly what is in my post #2 in here.

I know the interface for this is not the most intuitive in the world (a parameter for specifying a worksheet name would not go amiss), but that's what we're stuck with I'm afraid.

If you explain what you find confusing I can hopefully rephrase it (My explanations are not always the easiest to understand I know - even though I do try to make them so).

You'll see this isn''t simply about the code. It also relies on items existing in your database before we start.
Oct 28 '08 #4

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

Similar topics

4
3390
by: Anthony Cuttitta Jr. | last post by:
I'm working on some procedures where Access queries are exported to Excel, and then later on, those same workbooks are openned, and I need to target a specific original sheet. Sometimes there will...
0
1156
by: Nik | last post by:
I have a VB.NET Web application. I have to export data from SQL Server to excel. I have 3 Select statements and the results need to go in 3 separate worksheets and the column headers of the sheets...
14
6414
by: bonehead | last post by:
Greetings, I'm using the DoCmd.TransferText method to export the results of a MS Access query to a csv file. The csv will then be used to load an Oracle table. In other systems such as TOAD...
4
8695
by: paul.chae | last post by:
I have a table in Access with about 3000 records. There are ~60 unique values in the ID field for the 3000 records. What I would like to do is automatically generate multiple Excel worksheets...
0
2413
by: Tony2299 | last post by:
How to export data using ASP to excel workbook (with multiple worksheets)? I created an excel workbook with 5 worksheets and each one has a name range. Using asp I insert data to individual...
7
7778
by: Vanessa | last post by:
hi Everyone, I have two questions on exporting data to Excel using ASP (w/o converting formatted excel file into web page and then plug in the dynamic data): 1. Can we export data into...
2
5541
by: hal | last post by:
Hello all, I've been searching all day for an article or tutorial on how to get data from a SQL Server 2000 database and export the data to excel 2003 so that multiple worksheets are created,...
4
10486
by: Chris Gilpin | last post by:
Hey everybody. I have Access set up using VBA to run a bunch of queries automatically, and then export the results into Excel. The only problem is, I want each query to be exported to the same...
1
10467
by: CoolFactor | last post by:
MY CODE IS NEAR THE BOTTOM I want to export this Access query into Excel using a command button on an Access form in the following way I describe below. Below you will find the simple query I am...
0
7199
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7273
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,...
1
6982
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7451
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...
1
5000
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4667
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3161
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1501
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.