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: -
-
Dim FirstDayName As String
-
Dim i1 As Integer
-
Dim CurrentDate As Date
-
Dim HasBeenPopued As Integer
-
Dim S1, S2, D1, D2 As Integer
-
-
Option Compare Database
-
-
Private Sub AddData()
-
-
'Populating the report with information'
-
D1 = 1
-
-
Do While D1 <= 7
-
-
DayNumber = CStr(D1)
-
DateName = "D" + DayNumber + "Date"
-
-
If IsDate(Me.Controls(DateName)) Then
-
If DCount("*", "ManHoursQ", "[Dt] = " + DateName) > 0 Then
-
-
S1 = 1
-
-
Do While S1 <= 3
-
-
ShiftNumber = CStr(S1)
-
ShiftName = "D" + DayNumber + "S" + ShiftNumber
-
-
If DCount("*", "ManHoursQ", "[Dt] = " + DateName + " AND [Shift] = " + ShiftName) > 0 Then
-
-
MHOName = "D" + DayNumber + "S" + ShiftNumber + "MHO"
-
MHDName = "D" + DayNumber + "S" + ShiftNumber + "MHD"
-
ADMNName = "D" + DayNumber + "S" + ShiftNumber + "ADMN"
-
MCTName = "D" + DayNumber + "S" + ShiftNumber + "MCT"
-
COMPName = "D" + DayNumber + "S" + ShiftNumber + "COMP"
-
QLName = "D" + DayNumber + "S" + ShiftNumber + "QL"
-
LABName = "D" + DayNumber + "S" + ShiftNumber + "LAB"
-
MNTName = "D" + DayNumber + "S" + ShiftNumber + "MNT"
-
ENGRName = "D" + DayNumber + "S" + ShiftNumber + "ENGR"
-
FACName = "D" + DayNumber + "S" + ShiftNumber + "FAC"
-
EXECName = "D" + DayNumber + "S" + ShiftNumber + "EXEC"
-
ACCTName = "D" + DayNumber + "S" + ShiftNumber + "ACCT"
-
ITName = "D" + DayNumber + "S" + ShiftNumber + "IT"
-
HRName = "D" + DayNumber + "S" + ShiftNumber + "HR"
-
SCName = "D" + DayNumber + "S" + ShiftNumber + "SC"
-
RNDName = "D" + DayNumber + "S" + ShiftNumber + "RND"
-
CUSTName = "D" + DayNumber + "S" + ShiftNumber + "CUST"
-
TOCName = "D" + DayNumber + "S" + ShiftNumber + "TOC"
-
MHFRName = "D" + DayNumber + "S" + ShiftNumber + "MHFR"
-
COFFName = "D" + DayNumber + "S" + ShiftNumber + "COFF"
-
NCNName = "D" + DayNumber + "S" + ShiftNumber + "NCN"
-
COMMName = "D" + DayNumber + "S" + ShiftNumber + "COMM"
-
-
-
-
If IsDate(Me.Controls(DateName)) Then
-
CritStr = "[Dt] = " + DateName + " AND [Shift] = " + ShiftName
-
Me.Controls(MHOName) = DSum("[MHOrd]", "ManHoursQ", CritStr)
-
Me.Controls(MHDName) = DSum("[MHDel]", "ManHoursQ", CritStr)
-
Me.Controls(ADMNName) = DSum("[Admin]", "ManHoursQ", CritStr)
-
Me.Controls(MCTName) = DSum("[MaterialControl]", "ManHoursQ", CritStr)
-
Me.Controls(COMPName) = DSum("[Compounding]", "ManHoursQ", CritStr)
-
Me.Controls(QLName) = DSum("[Quality]", "ManHoursQ", CritStr)
-
Me.Controls(LABName) = DSum("[Laboratory]", "ManHoursQ", CritStr)
-
Me.Controls(MNTName) = DSum("[Maintenance]", "ManHoursQ", CritStr)
-
Me.Controls(ENGRName) = DSum("[Engineering]", "ManHoursQ", CritStr)
-
Me.Controls(FACName) = DSum("[Facilities]", "ManHoursQ", CritStr)
-
Me.Controls(EXECName) = DSum("[Executive]", "ManHoursQ", CritStr)
-
Me.Controls(ACCTName) = DSum("[Accounting]", "ManHoursQ", CritStr)
-
Me.Controls(ITName) = DSum("[IT]", "ManHoursQ", CritStr)
-
Me.Controls(HRName) = DSum("[HR]", "ManHoursQ", CritStr)
-
Me.Controls(SCName) = DSum("[SupplyChain]", "ManHoursQ", CritStr)
-
Me.Controls(RNDName) = DSum("[RND]", "ManHoursQ", CritStr)
-
Me.Controls(CUSTName) = DSum("[CustomerService]", "ManHoursQ", CritStr)
-
Me.Controls(TOCName) = DSum("[TotalOnClock]", "ManHoursQ", CritStr)
-
Me.Controls(MHFRName) = DSum("[MHFR]", "ManHoursQ", CritStr)
-
Me.Controls(COFFName) = DSum("[COffs]", "ManHoursQ", CritStr)
-
Me.Controls(NCNName) = DSum("[NCNs]", "ManHoursQ", CritStr)
-
-
If [Forms]![ManHoursRepGen]![AgencyCombo] = "All" Then
-
Else
-
Me.Controls(COMMName) = DLookup("[Comments]", "ManHoursQ", CritStr)
-
End If
-
-
End If
-
-
End If
-
-
S1 = S1 + 1
-
-
Loop
-
-
End If
-
-
End If
-
-
D1 = D1 + 1
-
-
Loop
-
-
HasBeenPopued = 1
-
-
End Sub
-
-
Private Sub InitiateReport()
-
-
Dim i2 As Integer
-
-
'Clearing All Date Fields'
-
i2 = 1
-
Do While i2 <= 7
-
DayNumber = CStr(i2)
-
ControlName = "D" + DayNumber + "Date"
-
Me.Controls(ControlName) = ""
-
i2 = i2 + 1
-
Loop
-
-
'Figures out the Week name and number of the First and last day of the month'
-
CurrentDate = GWk1StartDate
-
FirstDayName = WeekdayName(Weekday(CurrentDate))
-
-
-
If FirstDayName = "Sunday" Then
-
i1 = 1
-
End If
-
-
If FirstDayName = "Monday" Then
-
i1 = 2
-
End If
-
-
If FirstDayName = "Tuesday" Then
-
i1 = 3
-
End If
-
-
If FirstDayName = "Wednesday" Then
-
i1 = 4
-
End If
-
-
If FirstDayName = "Thursday" Then
-
i1 = 5
-
End If
-
-
If FirstDayName = "Friday" Then
-
i1 = 6
-
End If
-
-
If FirstDayName = "Saturday" Then
-
i1 = 7
-
End If
-
-
'Setting Dates'
-
Do While i1 <= 7
-
DayNumber = CStr(i1)
-
ControlName = "D" + DayNumber + "Date"
-
Me.Controls(ControlName).Value = CurrentDate
-
CurrentDate = CurrentDate + 1
-
i1 = i1 + 1
-
Loop
-
-
'Setting Shift Number'
-
-
D2 = 1
-
-
Do While D2 <= 7
-
-
S2 = 1
-
-
Do While S2 <= 3
-
ShiftName2 = "D" + CStr(D2) + "S" + CStr(S2)
-
Me.Controls(ShiftName2) = CStr(S2)
-
S2 = S2 + 1
-
Loop
-
-
D2 = D2 + 1
-
Loop
-
-
End Sub
-
-
Private Sub Report_Activate()
-
-
-
-
End Sub
-
-
-
Private Sub Report_Click()
-
-
If HasBeenPopued = 0 Then
-
-
Call AddData
-
-
End If
-
-
End Sub
-
-
Private Sub Report_GotFocus()
-
-
End Sub
-
-
Private Sub Report_Load()
-
-
Call InitiateReport
-
-
End Sub
-
-
-
Private Sub Report_Open(Cancel As Integer)
-
-
HasBeenPopued = 0
-
-
End Sub
-
-
9 1389
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.
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: - SELECT
-
Format([Dt],"dd") AS [Day]
-
, ManHours.Shift
-
, Sum(ManHours.MHOName) AS SumOfMHOName
-
, Sum(ManHours.MHDName) AS SumOfMHDName
-
, Sum(ManHours.ADMNName) AS SumOfADMNName
-
FROM ManHours
-
GROUP BY Format([Dt],"dd"), ManHours.Shift;
-
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: - SELECT Format([TestDate],"yyyy") AS [Year],
-
Format([TestDate],"mmmm") AS [Month],
-
Weekday([Testdate]) AS DayOfWeek,
-
Day([TestDate]) AS MonthDay,
-
-Int(([DayOfWeek]-[MonthDay]+1)/7)+1 AS Week
-
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: - Year Month DayOfWeek MonthDay Week
-
2014 August 6 1 1
-
2014 August 7 2 1
-
2014 August 1 3 2
-
2014 August 2 4 2
-
2014 August 3 5 2
-
2014 August 4 6 2
-
2014 August 5 7 2
-
2014 August 6 8 2
-
2014 August 7 9 2
-
2014 August 1 10 3
-
2014 August 2 11 3
-
2014 August 3 12 3
-
2014 August 4 13 3
-
2014 August 5 14 3
-
2014 August 6 15 3
-
2014 August 7 16 3
-
2014 August 1 17 4
-
2014 August 2 18 4
-
2014 August 3 19 4
-
2014 August 4 20 4
-
2014 August 5 21 4
-
2014 August 6 22 4
-
2014 August 7 23 4
-
2014 August 1 24 5
-
2014 August 2 25 5
-
2014 August 3 26 5
-
2014 August 4 27 5
-
2014 August 5 28 5
-
2014 August 6 29 5
-
2014 August 7 30 5
-
2014 August 1 31 6
Then, use your query results to poulate your report. It will be much wickedlier faster!!!
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.
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.
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.
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: -
-
SELECT Format([Dt],"yyyy") AS [Year]
-
, Format([Dt],"mmmm") AS [Month]
-
, Weekday([Dt]) AS DayOfWeek
-
, Day([Dt]) AS MonthDay
-
, -Int(([DayOfWeek]-[MonthDay]+1)/7)+1 AS Week
-
, ManHours.Shift
-
, Sum(ManHours.[MHOrd]) AS SumMHO
-
, Sum(ManHours.MHDel) AS SumMHD
-
, Sum(ManHours.Admin) AS SumOfADMN
-
, Sum(ManHours.MaterialControl) AS SumOfMC
-
, Sum(ManHours.Compounding) AS SumOfCOMP
-
, Sum(ManHours.Quality) AS SumOfQL
-
, Sum(ManHours.Laboratory) AS SumOfLAB
-
, Sum(ManHours.Maintenance) AS SumOfMAINT
-
, Sum(ManHours.Engineering) AS SumOfENGR
-
, Sum(ManHours.Facilities) AS SumOfFAC
-
, Sum(ManHours.Executive) AS SumOfEXEC
-
, Sum(ManHours.Accounting) AS SumOfACCT
-
, Sum(ManHours.IT) AS SumOfIT
-
, Sum(ManHours.HR) AS SumOfHR
-
, Sum(ManHours.SupplyChain) AS SumOfSuC
-
, Sum(ManHours.[RND]) AS SumOfRND
-
, Sum(ManHours.CustomerService) AS SumOfCS
-
, Sum(ManHours.TotalOnClock) AS SumOfTOC
-
, Sum(ManHours.MHFR) AS SumOfMHFR
-
, Sum(ManHours.COffs) AS SumOfCOffs
-
, Sum(ManHours.NCNs) AS SumOfNCNs
-
FROM ManHours
-
GROUP BY Format([Dt],"yyyy"), Format([Dt],"mmmm"), Weekday([Dt]), Day([Dt]), -Int(([DayOfWeek]-[MonthDay]+1)/7)+1, ManHours.Shift;
-
-
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?
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.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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;-)."...
|
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...
|
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...
|
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
...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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: 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...
| |