Ok, there will be a lot of parts to this, but hang in there.
First, we'll set up the form, where the user will enter the week starting date, pick an employee, and hit a button. Then we'll create the report.
1) On the form frmReport:
a) Put a text box called txtStartDate. Make the format be m/d/yy.
b) Put a combo box called cboEmployee, with the row source being
Code:
SELECT Employee.EmployeeID, [LastName] & ", " & [FirstName] AS EmpName FROM Employee;
column count = 2
column widths = 0,1
bound column = 1
c) Put a button on the form, named btnViewEmpReport. In the On Click property, choose [Event Procedure] and click the build button (the little button with the three dots on it to the right). Paste this code in between the SUB and END SUB lines:
Code:
DoCmd.OpenReport "WeeklyTimeSheet", acViewPreview, , "EmployeeID = " & Me.cboEmployee
This will open your report for the selected employee only.
Close and save the form.
2) Now let's make the report. Start a new report, and in design mode make the RecordSource be this:
Code:
TRANSFORM Sum(EmployeeHours.Hours) AS SumOfHours SELECT Employee.EmployeeID, EmployeeHours.ProjectID, Projects.ProjectName FROM (Employee INNER JOIN EmployeeHours ON Employee.EmployeeID=EmployeeHours.EmployeeID) INNER JOIN Projects ON EmployeeHours.ProjectID=Projects.ProjectID GROUP BY Employee.EmployeeID, EmployeeHours.ProjectID, Projects.ProjectName ORDER BY EmployeeHours.ProjectID PIVOT Format(Weekday([DateWorked]),"ddd") In ("Mon","Tue","Wed","Thu","Fri") WITH OWNERACCESS OPTION;
If you then look at this query in design view, and run it, you'll see that it shows every employee, every project, for MON, TUE, WED, THU, FRI. But don't worry, it'll open for only the employee you select on the frmReport.
View the field list, and bring the controls for ProjectName and Mon, Tue, Wed, Thu, Fri down into the report. Arrange them in the detail section. Remove the labels and put them in the header (select them, then cut and paste). Also in the header, in order to put something like "Weekly Time Sheet for Tom Smith for Week Starting 9/12/05", make a text box and put this:
Code:
="Time Card for " & Forms!frmReport!cboEmployee.column(1) & " for Week Starting " & Forms!frmReport!txtStartDate
. See how it references the frmReport? It will take the start date and emp name right off of that form.
Now, to put the dates above each MON, TUE, WED, THU, FRI label in the header, put one text box above MON and in it put
Code:
=Forms!frmReport!txtStartDate
This puts the start date above Monday.
For the rest, use the Dateadd function, so like above TUE, put a text box and in it put
Code:
=DateAdd("d",1,Forms!frmReport!txtStartDate)
. This will add 1 to the Start Date. Do the same for the other days, adding 2, then 3, then 4.
For the TOTALS column, make another text box to the far right. In the control source, put this:
Code:
=nz([Mon],0)+nz([Tue],0)+nz([Wed],0)+nz([Thu],0)+nz([FRI],0)
The NZ function makes a value be ZERO (or whatever you put after the comma) in case it's NULL. If things are null, they can't add up. So if you just put MON+TUE+WED+THU+FRI, and one of them had NULL for hours, it wouldn't add up. ok?
Ok--now for the sums at the bottom. We'll put them in the EmployeeID footer. To do this, go VIEW + Sorting and Grouping. Pick field EmployeeID, and down below set Group Footer to YES. Back in the design of the report, put a text box in the EmployeeID footer. In the control Source, put this:
Code:
=DSum("Hours","EmployeeHours","EmployeeID = " & Forms!frmReport!cboEmployee & " and DateWorked between #" & Forms!frmReport!txtStartDate & "# and #" & DateAdd("d",5,Forms!frmReport!txtStartDate) & "# and WorkCodeID = 2")
DSum, DLookup, etc are functions that can get data from a table that is not the direct source of your report. So this one that I wrote, sums up the hours in the table EmployeeHours for the selected Employee for the dates between your entered Start Date and 5 days after that, for WorkCodeID 2. Since you didn't note what data types your fields are, I assumed that WorkCodeID was a number. In my data, WorkCodeID 2 is OT. So I'd make the label say "OT:", and put that Dsum code into the corresponsing text box. You can add others for Reg Time, Sick Leave, etc, and just change the WorkCodeID = X portion to reflect the ID Code you need.
When you're done, close the report (Save it as "WeeklyTimeSheet"). Open the form, enter a MONDAY date, select an employee, and hit the button.
Ok, so that's a lot!! Try it out and let us know how far you go.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244. Basics at