473,411 Members | 2,186 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,411 software developers and data experts.

Populating weekly reports

9
Hi,

So I am working on this project where I have to generate weekly reports for an entire month Based on the inputs of Month and Year.

So For example If I specify: August 2014, the form figures out there are 6 weeks in August and then generate 6 reports (1 for each weeks with specific data for each week).

The way I am currently doing this is by running some nested loops to go through each day of the week, figure out if there is data and then populate each text box. (I have attached a PDF of the report so you can get an idea of what is looks like, Each box is a unique text box).

The problem I am having is that the reports take a long time to load (I know this is caused by the loops). I was trying to figure out if there is a faster way to do this? Thank you.

The code I am using is:

Expand|Select|Wrap|Line Numbers
  1.  
  2. Dim FirstDayName As String
  3. Dim i1 As Integer
  4. Dim CurrentDate As Date
  5. Dim HasBeenPopued As Integer
  6. Dim S1, S2, D1, D2 As Integer
  7.  
  8. Option Compare Database
  9.  
  10. Private Sub AddData()
  11.  
  12. 'Populating the report with information'
  13. D1 = 1
  14.  
  15. Do While D1 <= 7
  16.  
  17.     DayNumber = CStr(D1)
  18.     DateName = "D" + DayNumber + "Date"
  19.  
  20.     If IsDate(Me.Controls(DateName)) Then
  21.         If DCount("*", "ManHoursQ", "[Dt] = " + DateName) > 0 Then
  22.  
  23.             S1 = 1
  24.  
  25.             Do While S1 <= 3
  26.  
  27.                 ShiftNumber = CStr(S1)
  28.                 ShiftName = "D" + DayNumber + "S" + ShiftNumber
  29.  
  30.                 If DCount("*", "ManHoursQ", "[Dt] = " + DateName + " AND [Shift] = " + ShiftName) > 0 Then
  31.  
  32.                     MHOName = "D" + DayNumber + "S" + ShiftNumber + "MHO"
  33.                     MHDName = "D" + DayNumber + "S" + ShiftNumber + "MHD"
  34.                     ADMNName = "D" + DayNumber + "S" + ShiftNumber + "ADMN"
  35.                     MCTName = "D" + DayNumber + "S" + ShiftNumber + "MCT"
  36.                     COMPName = "D" + DayNumber + "S" + ShiftNumber + "COMP"
  37.                     QLName = "D" + DayNumber + "S" + ShiftNumber + "QL"
  38.                     LABName = "D" + DayNumber + "S" + ShiftNumber + "LAB"
  39.                     MNTName = "D" + DayNumber + "S" + ShiftNumber + "MNT"
  40.                     ENGRName = "D" + DayNumber + "S" + ShiftNumber + "ENGR"
  41.                     FACName = "D" + DayNumber + "S" + ShiftNumber + "FAC"
  42.                     EXECName = "D" + DayNumber + "S" + ShiftNumber + "EXEC"
  43.                     ACCTName = "D" + DayNumber + "S" + ShiftNumber + "ACCT"
  44.                     ITName = "D" + DayNumber + "S" + ShiftNumber + "IT"
  45.                     HRName = "D" + DayNumber + "S" + ShiftNumber + "HR"
  46.                     SCName = "D" + DayNumber + "S" + ShiftNumber + "SC"
  47.                     RNDName = "D" + DayNumber + "S" + ShiftNumber + "RND"
  48.                     CUSTName = "D" + DayNumber + "S" + ShiftNumber + "CUST"
  49.                     TOCName = "D" + DayNumber + "S" + ShiftNumber + "TOC"
  50.                     MHFRName = "D" + DayNumber + "S" + ShiftNumber + "MHFR"
  51.                     COFFName = "D" + DayNumber + "S" + ShiftNumber + "COFF"
  52.                     NCNName = "D" + DayNumber + "S" + ShiftNumber + "NCN"
  53.                     COMMName = "D" + DayNumber + "S" + ShiftNumber + "COMM"
  54.  
  55.  
  56.  
  57.                     If IsDate(Me.Controls(DateName)) Then
  58.                         CritStr = "[Dt] = " + DateName + " AND [Shift] = " + ShiftName
  59.                         Me.Controls(MHOName) = DSum("[MHOrd]", "ManHoursQ", CritStr)
  60.                         Me.Controls(MHDName) = DSum("[MHDel]", "ManHoursQ", CritStr)
  61.                         Me.Controls(ADMNName) = DSum("[Admin]", "ManHoursQ", CritStr)
  62.                         Me.Controls(MCTName) = DSum("[MaterialControl]", "ManHoursQ", CritStr)
  63.                         Me.Controls(COMPName) = DSum("[Compounding]", "ManHoursQ", CritStr)
  64.                         Me.Controls(QLName) = DSum("[Quality]", "ManHoursQ", CritStr)
  65.                         Me.Controls(LABName) = DSum("[Laboratory]", "ManHoursQ", CritStr)
  66.                         Me.Controls(MNTName) = DSum("[Maintenance]", "ManHoursQ", CritStr)
  67.                         Me.Controls(ENGRName) = DSum("[Engineering]", "ManHoursQ", CritStr)
  68.                         Me.Controls(FACName) = DSum("[Facilities]", "ManHoursQ", CritStr)
  69.                         Me.Controls(EXECName) = DSum("[Executive]", "ManHoursQ", CritStr)
  70.                         Me.Controls(ACCTName) = DSum("[Accounting]", "ManHoursQ", CritStr)
  71.                         Me.Controls(ITName) = DSum("[IT]", "ManHoursQ", CritStr)
  72.                         Me.Controls(HRName) = DSum("[HR]", "ManHoursQ", CritStr)
  73.                         Me.Controls(SCName) = DSum("[SupplyChain]", "ManHoursQ", CritStr)
  74.                         Me.Controls(RNDName) = DSum("[RND]", "ManHoursQ", CritStr)
  75.                         Me.Controls(CUSTName) = DSum("[CustomerService]", "ManHoursQ", CritStr)
  76.                         Me.Controls(TOCName) = DSum("[TotalOnClock]", "ManHoursQ", CritStr)
  77.                         Me.Controls(MHFRName) = DSum("[MHFR]", "ManHoursQ", CritStr)
  78.                         Me.Controls(COFFName) = DSum("[COffs]", "ManHoursQ", CritStr)
  79.                         Me.Controls(NCNName) = DSum("[NCNs]", "ManHoursQ", CritStr)
  80.  
  81.                         If [Forms]![ManHoursRepGen]![AgencyCombo] = "All" Then
  82.                         Else
  83.                             Me.Controls(COMMName) = DLookup("[Comments]", "ManHoursQ", CritStr)
  84.                         End If
  85.  
  86.                     End If
  87.  
  88.                 End If
  89.  
  90.                 S1 = S1 + 1
  91.  
  92.             Loop
  93.  
  94.         End If
  95.  
  96.     End If
  97.  
  98.     D1 = D1 + 1
  99.  
  100. Loop
  101.  
  102. HasBeenPopued = 1
  103.  
  104. End Sub
  105.  
  106. Private Sub InitiateReport()
  107.  
  108. Dim i2 As Integer
  109.  
  110. 'Clearing All Date Fields'
  111. i2 = 1
  112. Do While i2 <= 7
  113.     DayNumber = CStr(i2)
  114.     ControlName = "D" + DayNumber + "Date"
  115.     Me.Controls(ControlName) = ""
  116.     i2 = i2 + 1
  117. Loop
  118.  
  119. 'Figures out the Week name and number of the First and last day of the month'
  120. CurrentDate = GWk1StartDate
  121. FirstDayName = WeekdayName(Weekday(CurrentDate))
  122.  
  123.  
  124. If FirstDayName = "Sunday" Then
  125.     i1 = 1
  126. End If
  127.  
  128. If FirstDayName = "Monday" Then
  129.     i1 = 2
  130. End If
  131.  
  132. If FirstDayName = "Tuesday" Then
  133.     i1 = 3
  134. End If
  135.  
  136. If FirstDayName = "Wednesday" Then
  137.     i1 = 4
  138. End If
  139.  
  140. If FirstDayName = "Thursday" Then
  141.     i1 = 5
  142. End If
  143.  
  144. If FirstDayName = "Friday" Then
  145.     i1 = 6
  146. End If
  147.  
  148. If FirstDayName = "Saturday" Then
  149.     i1 = 7
  150. End If
  151.  
  152. 'Setting Dates'
  153. Do While i1 <= 7
  154.     DayNumber = CStr(i1)
  155.     ControlName = "D" + DayNumber + "Date"
  156.     Me.Controls(ControlName).Value = CurrentDate
  157.     CurrentDate = CurrentDate + 1
  158.     i1 = i1 + 1
  159. Loop
  160.  
  161. 'Setting Shift Number'
  162.  
  163. D2 = 1
  164.  
  165. Do While D2 <= 7
  166.  
  167. S2 = 1
  168.  
  169.     Do While S2 <= 3
  170.         ShiftName2 = "D" + CStr(D2) + "S" + CStr(S2)
  171.         Me.Controls(ShiftName2) = CStr(S2)
  172.         S2 = S2 + 1
  173.     Loop
  174.  
  175. D2 = D2 + 1
  176. Loop
  177.  
  178. End Sub
  179.  
  180. Private Sub Report_Activate()
  181.  
  182.  
  183.  
  184. End Sub
  185.  
  186.  
  187. Private Sub Report_Click()
  188.  
  189. If HasBeenPopued = 0 Then
  190.  
  191.     Call AddData
  192.  
  193. End If
  194.  
  195. End Sub
  196.  
  197. Private Sub Report_GotFocus()
  198.  
  199. End Sub
  200.  
  201. Private Sub Report_Load()
  202.  
  203. Call InitiateReport
  204.  
  205. End Sub
  206.  
  207.  
  208. Private Sub Report_Open(Cancel As Integer)
  209.  
  210. HasBeenPopued = 0
  211.  
  212. End Sub
  213.  
  214.  
Attached Files
File Type: pdf ManHoursWeeklyRepWk1.pdf (188.4 KB, 192 views)
Oct 9 '14 #1
9 1389
Rabbit
12,516 Expert Mod 8TB
Use a query to return the data needed for each day of the month. There's no need for a lookup query for every single control. That's like running hundreds of separate queries to populate everything when all you need is 1.

The query just needs to return a week number and a day number for each row. Then you aggregate all your data by the week and day number. If you want blank rows for where there is no data but still want to represent it in the report, you just need to outer join a recordset that has all the combinations of week numbers and day numbers.

This is the general gist of what you will need to do. If you need further detail, we will need to know the table structures a lot better.
Oct 9 '14 #2
jforbes
1,107 Expert 1GB
If you really want to speed things up, you can use a Aggregate Query (A Select Query with a Group By clause) as the basis of your Report. I think the trick that needs to be figured out is how to Format the Date so that it gets Grouped correctly. It's been a while since I've done this, so something like:
Expand|Select|Wrap|Line Numbers
  1. SELECT 
  2.   Format([Dt],"dd") AS [Day]
  3. , ManHours.Shift
  4. , Sum(ManHours.MHOName) AS SumOfMHOName
  5. , Sum(ManHours.MHDName) AS SumOfMHDName
  6. , Sum(ManHours.ADMNName) AS SumOfADMNName
  7. FROM ManHours
  8. GROUP BY Format([Dt],"dd"), ManHours.Shift;
  9.  
Oct 9 '14 #3
twinnyfo
3,653 Expert Mod 2GB
somar,

I think the key you are looking for is how to get the number of week within each month.

Using the advice from both Rabbit and JForbes, combine their solutions, using the code below for your query:

Expand|Select|Wrap|Line Numbers
  1. SELECT Format([TestDate],"yyyy") AS [Year], 
  2.     Format([TestDate],"mmmm") AS [Month], 
  3.     Weekday([Testdate]) AS DayOfWeek, 
  4.     Day([TestDate]) AS MonthDay, 
  5.     -Int(([DayOfWeek]-[MonthDay]+1)/7)+1 AS Week
  6. FROM tblWorkHours;
Just replace "tblWorkHours" with your Table Name and "TestDate" with the name of your field that contains the date.

Here are the results for August:

Expand|Select|Wrap|Line Numbers
  1. Year  Month   DayOfWeek  MonthDay  Week
  2. 2014  August  6          1           1
  3. 2014  August  7          2           1
  4. 2014  August  1          3           2
  5. 2014  August  2          4           2
  6. 2014  August  3          5           2
  7. 2014  August  4          6           2
  8. 2014  August  5          7           2
  9. 2014  August  6          8           2
  10. 2014  August  7          9           2
  11. 2014  August  1          10          3
  12. 2014  August  2          11          3
  13. 2014  August  3          12          3
  14. 2014  August  4          13          3
  15. 2014  August  5          14          3
  16. 2014  August  6          15          3
  17. 2014  August  7          16          3
  18. 2014  August  1          17          4
  19. 2014  August  2          18          4
  20. 2014  August  3          19          4
  21. 2014  August  4          20          4
  22. 2014  August  5          21          4
  23. 2014  August  6          22          4
  24. 2014  August  7          23          4
  25. 2014  August  1          24          5
  26. 2014  August  2          25          5
  27. 2014  August  3          26          5
  28. 2014  August  4          27          5
  29. 2014  August  5          28          5
  30. 2014  August  6          29          5
  31. 2014  August  7          30          5
  32. 2014  August  1          31          6
Then, use your query results to poulate your report. It will be much wickedlier faster!!!
Oct 9 '14 #4
somar
9
So the issue isn't figuring out the dates and the week number, that I have figured out (And I may not be understanding what you are trying to explain here, I am pretty new at using access, and not very adept at it). The trouble I am having is figuring out how to populate the query with this and also how would I add code to a query (I know how to set criteria).

As Rabbit suggested I decided to upload the database I am working on so you guys can get a better idea of my table, report, and query is setup.

Thanks a lot for your responses so far, I really appreciate the help.
Oct 10 '14 #5
zmbd
5,501 Expert Mod 4TB
somar
We do ask that you make the effort and show your work, attaching the database file is not usually needed nor desirable as most of the experts provide our help while at work; thus, usually unable to open the file.

Providing the detail for the tables in question can be provided:
[tbl_one]
[PK](autonumber)
[fields...}

[tbl_two]
[PK](autonumber)
[fk_tbl_one](numeric-long)
[example_name](text(25))
[fields...}

etc...

Writing Queries is a very basic skill set that you must master in order to really benefit from any database. Please work thru the following tutorial: Create queries for a new database

This is also a site that covers database development fairly well... please follow the link to the ACC2003 to learn about normalization and proper table setups... microsoft office access 2010 - power tutorials

Once you have written your SQL (either via the GUI-Query Editor or by using the SQL-View), please, cut and paste it here, format it using the [CODE/] button in the toolbar and we can most easily point you in the right direction.

As for writing "code" behind a query, that is a strange statement. One can write custom functions and use them within a query; however, that is the extent within Access.
Oct 10 '14 #6
twinnyfo
3,653 Expert Mod 2GB
somar,

I did get a chance to look at your reports, and they are designed very poorly and will cause you many problems as you continue with this project. You have six identical reports, one for each potential week of the month and each report has hundreds of controls. This is a poor design. You need to redesign the reports so that each set of data cascades through the days of the weeks (even down to the shifts).

Each detail section of the report should cover one shift. You should have multiple header/footer combinations, with sums included in the footer.

Your summing groups should be:

Shift, Day, Week, Month, Year (if you have an annual report).

This can all be done on ONE report, setting parameters for the dates covered in you report generating form. I've played around with it, and it is doable, using the formulas we have provided thus far.

Let us know if you come across any snags.
Oct 13 '14 #7
somar
9
Hi, Thanks for taking a look at it. Yeah, I realize that this is a pretty bad design and that is the reason I am trying to get help to do this.

I think I understand how to make the get the report to populate by using the query (without running loops), what I am struggling with right now is figuring out how to make the report leave the blank fields for dates that do not contain any data (for example if there is no data for 8/3/2014, it should still show blanks for that date).

So I combined twinnyfo and jforbes code together to come up with this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT Format([Dt],"yyyy") AS [Year]
  3. , Format([Dt],"mmmm") AS [Month]
  4. , Weekday([Dt]) AS DayOfWeek
  5. , Day([Dt]) AS MonthDay
  6. , -Int(([DayOfWeek]-[MonthDay]+1)/7)+1 AS Week
  7. , ManHours.Shift
  8. , Sum(ManHours.[MHOrd]) AS SumMHO
  9. , Sum(ManHours.MHDel) AS SumMHD
  10. , Sum(ManHours.Admin) AS SumOfADMN
  11. , Sum(ManHours.MaterialControl) AS SumOfMC
  12. , Sum(ManHours.Compounding) AS SumOfCOMP
  13. , Sum(ManHours.Quality) AS SumOfQL
  14. , Sum(ManHours.Laboratory) AS SumOfLAB
  15. , Sum(ManHours.Maintenance) AS SumOfMAINT
  16. , Sum(ManHours.Engineering) AS SumOfENGR
  17. , Sum(ManHours.Facilities) AS SumOfFAC
  18. , Sum(ManHours.Executive) AS SumOfEXEC
  19. , Sum(ManHours.Accounting) AS SumOfACCT
  20. , Sum(ManHours.IT) AS SumOfIT
  21. , Sum(ManHours.HR) AS SumOfHR
  22. , Sum(ManHours.SupplyChain) AS SumOfSuC
  23. , Sum(ManHours.[RND]) AS SumOfRND
  24. , Sum(ManHours.CustomerService) AS SumOfCS
  25. , Sum(ManHours.TotalOnClock) AS SumOfTOC
  26. , Sum(ManHours.MHFR) AS SumOfMHFR
  27. , Sum(ManHours.COffs) AS SumOfCOffs
  28. , Sum(ManHours.NCNs) AS SumOfNCNs
  29. FROM ManHours
  30. GROUP BY Format([Dt],"yyyy"), Format([Dt],"mmmm"), Weekday([Dt]), Day([Dt]), -Int(([DayOfWeek]-[MonthDay]+1)/7)+1, ManHours.Shift;
  31.  
  32.  
But as I said, it does not give me the blank dates. I was thinking of making a query that generated all the dates of a month and then combining it with this one, would that be a good way to go about it?
Oct 13 '14 #8
Rabbit
12,516 Expert Mod 8TB
Yes, you can do that to get the missing dates. Which is what I meant in the second paragraph of post #2. But you don't really need to create a "query" of valid dates. Just create a table with 31 rows numbered 1 to 31. Just remember to check first that the date that is created is a valid date. You can use the IsDate function for that.
Oct 13 '14 #9
somar
9
Hi Guys, I was finally able figure out how to get the reports to generate exactly how I needed them to without using any loops. Thanks a lot for all your help with this.
Oct 15 '14 #10

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

Similar topics

1
by: Craig Ringer | last post by:
QOTW: "Such infrastructure building is in fact fun and instructive -- as long as you don't fall into the trap of *using* such complications in production code, where Python's simplicity rules;-)."...
0
by: zexx | last post by:
Need some advise.... Payroll program Access97. Payroll is printed then posted to a hx table thro' a query with the payroll calculations that freeze that payroll's numbers for that period. The...
1
by: bmoos1 | last post by:
I have two reports: 2004 New Enrollments and 2004 Cancellations. 1. Each report shows by month: "Monthly", "BiMonthly"(*2), "Weekly" (*52/12), "Total Monthly" (++) and "Projected...
1
by: Cameron Laird | last post by:
QOTW: "People who are smart and care about correctness -- the 'reality-based community' -- often don't realise just how many decisions are made on the basis of unfacts ..." - Steven D'Aprano ...
0
by: Cameron Laird | last post by:
QOTW: "I'm a huge fan of single digit numbers ..." - Jim Hugunin, illustrating his undiminished grasp on the Pythonic ethos "It's hard to say exactly what constitutes research in the computer...
0
by: Paul Boddie | last post by:
QOTW: "I still want to keep compile time type checking to make sure I don't make any mistakes." "Sounds like you want two wives, but I'm pretty sure polygamy gets a checkbox in the naughty...
0
by: Cameron Laird | last post by:
QOTW: "Dictionaries are one of the most useful things in Python. Make sure you know how to take adavantage of them..." - Jeremy Sanders "Python has consistently failed to disappoint me." - Tal...
2
by: ksrashmi | last post by:
Hi , i want to validate the date range . it is weekly report . so exact date has to be enterd . pls help me in which way we need to validate weekly reports . is there any java script to...
3
by: Brock | last post by:
I am trying to populate a Crystal Report from data in my DataGrid. The reason for this is that I want the user to be able to change values without updating the database, but still have their...
4
by: Bre035 | last post by:
I am attempting to create several weekly reports that look at the last seven days exluding the current day, but I have not been able to successfull pull the correct information. I use the several...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...
0
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...

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.