473,396 Members | 1,858 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,396 software developers and data experts.

VBA to loop through records in a table as parameter inputs for a few queries, save to

I am looking for the vba to have a query cycle through using records from another query as the parameter run and export for each of the records on the second query.

Table 1 has information with all clients
query 1 has x, y, z being pulled > client used as parameter
query 2 has the list of clients

Currently I have the coding to run the query and export however I am not sure how to get it to repeat for each client.

Code:
Expand|Select|Wrap|Line Numbers
  1. Private Sub CmdInternalReports_Click()
  2. Dim xlApp As Excel.Application
  3.     Dim rstDetails As DAO.Recordset
  4.     Dim strTab As String
  5.     Dim strDir As String
  6.  
  7.     Set xlApp = Nothing
  8.     Set rstDetails = Nothing
  9.  
  10.     'Open and display the Network EMC Template
  11.     Set xlApp = New Excel.Application
  12.     'xlApp.Visible = True
  13.     xlApp.Workbooks.Open (SavePath & "Templates\Network EMC Report XCOL Template.xlsx")
  14.  
  15.     'Copy the Order has been scheduled delivered mmddyyyy
  16.     strTab = "EMC"
  17.     Set rstDetails = CurrentDb.OpenRecordset("qryXCLONetworkEMCSchedDelv")
  18.     xlApp.Worksheets(strTab).Select
  19.     xlApp.Worksheets(strTab).Cells(3, 1).CopyFromRecordset rstDetails
  20.     xlApp.Worksheets(strTab).Cells.Select
  21.     xlApp.Worksheets(strTab).Cells.EntireColumn.AutoFit
  22.     xlApp.Worksheets(strTab).Cells(1, 1).Select
  23.  
  24.  
  25. '    xlApp.ActiveWorkbook.SaveAs strDir & "\Order has been scheduled delivered mmddyyyy.xls"
  26. '    SetAttr strDir & "\Order has been scheduled delivered mmddyyyy.xls", vbReadOnly
  27.  
  28.     xlApp.Worksheets(1).Select
  29.  
  30.     xlApp.ActiveWorkbook.SaveAs "P:\DSC - Inventory Management\EMC and Open Orders\Reports to Work\Order has been scheduled delivered " & Format(Date, "mmddyyyy")
  31.     SetAttr "P:\DSC - Inventory Management\EMC and Open Orders\Reports to Work\Order has been scheduled delivered " & Format(Date, "mmddyyyy") & ".xlsx", vbReadOnly
  32.     'Close Excel
  33.     xlApp.ActiveWorkbook.Close
  34.     xlApp.Quit
  35.  
  36.     MsgBox "Done!"
  37. End Sub
The coding above runs two different queries and saves them to two seperate tabs on the pre-made template for this report.



Below is the SQL coding I currently have for the first query:

Code:
Expand|Select|Wrap|Line Numbers
  1. SELECT 
  2. tblImportNetworkEMC.DeliveryHub, 
  3. tblImportNetworkEMC.HubType, 
  4. tblImportNetworkEMC.Origin, 
  5. tblImportNetworkEMC.Client, 
  6. tblImportNetworkEMC.CurrentLoc, 
  7. tblImportNetworkEMC.Consignee, 
  8. tblImportNetworkEMC.Shipment, 
  9. tblImportNetworkEMC.Order, 
  10. tblImportNetworkEMC.WaitingFor, 
  11. tblImportNetworkEMC.TotalDaysAged, 
  12. tblImportNetworkEMC.IntLastDate, 
  13. tblImportNetworkEMC.IntEnterBy, 
  14. tblImportNetworkEMC.IntLastType, 
  15. tblImportNetworkEMC.InternalAge, 
  16. tblImportNetworkEMC.IntCommentText, 
  17. qryClientNetworkEMC_FilterCHAD.Shipment
  18.  
  19. FROM 
  20.   ((tblImportNetworkEMC 
  21.  LEFT JOIN 
  22.    qryClientNetworkEMC_Filter ON tblImportNetworkEMC.Shipment = qryClientNetworkEMC_Filter.Shipment) 
  23.  LEFT JOIN 
  24.    qryClientNetworkEMC_FilterCHAD ON tblImportNetworkEMC.Shipment = qryClientNetworkEMC_FilterCHAD.Shipment) 
  25.  INNER JOIN 
  26.    tblClientReport ON tblImportNetworkEMC.Client = tblClientReport.Client
  27.  
  28. WHERE (((tblImportNetworkEMC.WaitingFor)="Client") 
  29.   AND ((tblImportNetworkEMC.IntLastType)<>"XCLO") 
  30.   AND ((qryClientNetworkEMC_FilterCHAD.Shipment) Is Null) 
  31.   AND ((tblImportNetworkEMC.Status)="Active Delivery" 
  32.         Or (tblImportNetworkEMC.Status)="Active Closed Delivery") 
  33.   AND ((qryClientNetworkEMC_Filter.Shipment) Is Null));
  34.  
I would like to figure out how cycle through the records in a separate query

qryClient-NetworkEMCQuery.Client

as the parameter for

tblImportNetworkEMC.Client

There can be upto 70 different clients or as little as 2 depending on the day. So the Query I have takes a list of the clients that receive the report and runs it against the imported table to only give the ones with data day.

tblClientReport.Distro
Is where I have the report via client name linked the the distro list that should be pulled.

Any advise for how to look this up would be much appreciated.
Aug 11 '15 #1
0 992

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

Similar topics

1
by: jason | last post by:
Access 2000 I would like to be able to generate table reports listing all the TABLE NAMES, FIELDS and their datatypes in a neatly formatted table. Additionally I am building up a frightening...
2
by: Christine | last post by:
I am struggling to learn to use Access almost on my own, and have written a query that needs to have a parameter. The query I wrote includes in the Criteria of the design view, under the column...
2
by: DaveDiego | last post by:
I'm building a report that has a count of cases for employees. I have separate queries that count, OpenCases, NewCases, TotalCases, Etc. I would like to put all those counts into a record for each...
1
by: zek2005 | last post by:
Hi!!!! I´m stuck with a problem I have to make a table with the information of three different queries. The table has 4 columns x n rows. The first and second columns must be filled with the...
4
by: tarafinlay | last post by:
Hi all, I am new to access and am finding it a bit unintuitive having worked with SQL server in the past... And I am in a bit of a hurry because my employer wants me to crank something out which...
4
by: ManningFan | last post by:
Let's see if I can ask this without confusing you or myself. I need to be able to type a tablename into a box, and search my entire database to see which queries are using that table. Is this...
6
by: VivDenham | last post by:
Hi there I am brand new to VBA - a 61 year old lady who likes machine knitting. For the last couple of years, I have been designing an Access database called Knitting Database. I want a...
1
by: miss D | last post by:
hi, i have posted this question here, regarding Access SQL i believe someone here is good in SQL. anyone can help me? ...
8
by: Maverex | last post by:
I want to design a query that checks 2 other queries for overlapping planning times. I generate recurring events in a query. It generates dates and beginning times and ending times. There is also an...
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
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
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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
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.