Solved

Calculate Weekly Payroll Hours

Posted on 2003-11-21
11
2,741 Views
Last Modified: 2008-02-01
I have a payroll form with employees & their time in(emp1ti) & time out(emp1to) that is filled out daily.  I have an expression to calculate an employee's daily hours minus 1/2 hour for lunch in the control source property of a box used just for display: =(DateDiff("n",[emp1ti],[emp1to])/60)-0.5

I need to also display total weekly hours on the form.  I have a table field (emp1twh) that I would like to write the daily time as an integer to.  I would like to add to that field every day's daily hours until it is Monday again, then overwrite the field.  The new value should be displayed as soon as the new timein and timeout are entered.  I have the following psuedo code, but am unsure where to put it and how to update my table field:

If DatePart("d", emp1ti[,2]) = 2
   Then emp1twh=(DateDiff("n",[emp1ti],[emp1to])/60)-0.5
Else
    emp1twh=emp1twh+((DateDiff("n",[emp1ti],[emp1to])/60)-0.5)

Any help is much appreciated.  I am new to VBA.

Thanks,
jessica@tridewb.com
0
Comment
Question by:jlbillingsley
11 Comments
 
LVL 26

Expert Comment

by:Alan Warren
ID: 9800628
Hi Jessica,

Something like this may do:

SELECT [tblTimeClock].[IN], [tblTimeClock].[Out], Format(DateAdd("s",DateDiff("s",[In],([Out])),(Date())),"hh:nn:ss") AS Hours, "00:45:00" AS Lunch, Format(DateAdd("s",DateDiff("s",[In],([Out])),(Date()-#12/30/1899 0:45:0#)),"hh:nn:ss") AS Net
FROM tblTimeClock;


Just change the fieldnames:

IN > emp1ti
Out > emp1to
tblTimeClock > YourTable


Alan :)
0
 
LVL 26

Expert Comment

by:Alan Warren
ID: 9800636
My employees got 45 minutes for lunch.
Yours would be 00:30:00" AS Lunch

Alan :)
0
 
LVL 26

Expert Comment

by:Alan Warren
ID: 9800648
SELECT [tblTimeClock].[emp1ti], [tblTimeClock].[emp1to], Format(DateAdd("s",DateDiff("s",[emp1ti],([emp1to])),(Date())),"hh:nn:ss") AS Hours, "00:30:00" AS Lunch, Format(DateAdd("s",DateDiff("s",[emp1ti],([emp1to])),(Date()-#12/30/1899 0:30:0#)),"hh:nn:ss") AS emp1twh
FROM tblYourTable;
0
 
LVL 33

Expert Comment

by:Mike Eghtebas
ID: 9801051
A very good solusion Alan.  Mike
0
 

Author Comment

by:jlbillingsley
ID: 9804164
Where does this go?  I keep getting a syntax error.
0
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
LVL 26

Expert Comment

by:Alan Warren
ID: 9804680
Hi Jessica,

If you want to use it as a calculated field in a report or form add this to the controlsource property for the calculated field.
=Format(DateAdd("s",DateDiff("s",[emp1ti],([emp1to])),(Date()-#12/30/1899 0:30:0#)),"hh:nn:ss")

If you want to include it in the form or reports recordsource you need to edit the recordsource for the form or report by clicking the build button [...] alongside the Record Source property in the form/report properties dialog.This will invoke a query builder based on the current recordsource.

To add a calculated field in query design view, in a new field add:
emp1twh: Format(DateAdd("s",DateDiff("s",[emp1ti],([emp1to])),(Date()-#12/30/1899 0:30:0#)),"hh:nn:ss")

Then set the controlsource of your calculated field in your form/report to: emp1twh

If you are still having difficulty, thats OK, just let me know and we can take this one step at a time from the top.
Initially we aim to provide a quick response, treating questions with greater points associated with greater urgency. I am more than willing to walk through this with you.


Alan :)


0
 
LVL 8

Expert Comment

by:gajender_99
ID: 9809716
SELECT Table.id,sum(([Table]![emt1to]-[Table]![emp1ti])*24 - 0.5) AS Expr1
FROM Table
where table.id = 1
group by table.id;
0
 

Author Comment

by:jlbillingsley
ID: 9812206
Maybe I wasn't clear in my original question.  This appears to calculate only the hours between the employee's time-in & time-out.  My expression takes care of this.  I also like that my expression returns an integer number.  The part I need assistance with is a running total of total weekly hours worked on the form.  I want to add to the total weekly hours field until it is Monday again, and then I want to overwrite the field and start over.  I have the following pseudo code:

My fields are emp1ti(employee time in), emp1to(employee time out), and emp1twh(employee total weekly hours)

If DatePart("d", emp1ti[,2]) = 2
   Then emp1twh=(DateDiff("n",[emp1ti],[emp1to])/60)-0.5
Else
    emp1twh=emp1twh+((DateDiff("n",[emp1ti],[emp1to])/60)-0.5)

Thanks!
0
 

Author Comment

by:jlbillingsley
ID: 9819578
If it's Monday, (start over) write the integer difference between employee time in and employee time out minus 1/2 hour for lunch.
Otherwise, add what already exists in that table field: employee total weekly hours, to the integer difference between employee time in and employee time out minus 1/2 hour for lunch.
0
 
LVL 26

Accepted Solution

by:
Alan Warren earned 500 total points
ID: 9821295
Hi Jessica,

I think I've got the picture now!

You didnt mention whether employees had a unique ID, so I assumed that they did and called mine empID.
I also assumed the name of the table that your form is sourced on is called tblEmployees.
These two assumptions may need to modified to fit.

Here is a Sub Procedure that you can assign to the After Update Event for your form field emp1to.

So after a value is entered in emp1to this sub should fire and produce a message box showing hours worked for the current employee for the Week-Block beginning from the Monday prior to emp1ti to the Monday after emp1ti. allowing for the possibility that empt1i is a Monday. Phew!

Private Sub emp1to_AfterUpdate()
  Dim iWeekday As Integer
  Dim fDate As Date
  Dim tDate As Date
  Dim dTotalHoursWorked As Double
  Dim sWhere As String
  Dim sql As String
  Dim rs As ADODB.Recordset
 
  If Nz(emp1ti, 0) = 0 Then: Exit Sub
  If Nz(emp1to, 0) = 0 Then: Exit Sub
 
  Me.emp1tdh = (DateDiff("n", [emp1ti], [emp1to]) / 60) - 0.5
 
  ' Establish the range
  fDate = emp1ti
  iWeekday = Weekday(fDate)
 
  ' establish lower date range
  ' calculate date for previous monday from emp1ti
  ' if emp1ti is monday use emp1ti
  While iWeekday <> 2
    fDate = DateAdd("d", -1, fDate)
    iWeekday = Weekday(fDate)
  Wend
  fDate = Format(fDate, "dd-mmm-yyyy")
 
  ' establish upper date range
  tDate = Format(DateAdd("d", 7, fDate), "dd-mmm-yyyy")
 
  ' Build the query string
  ' Filter between date range (fDate and tdate) and EmpID
 
  sWhere = " WHERE (((tblEmployees.emp1ti) Between #" & fDate & "# And #" & tDate & "#"
  sWhere = sWhere & ") AND ((tblEmployees.empID)=" & empID & "))"
  sql = "SELECT Sum((DateDiff('n',[emp1ti],[emp1to])/60)-0.5) AS calcDailyHours From tblEmployees"
  sql = sql & sWhere
 
  Set rs = New ADODB.Recordset
  rs.Open sql, CurrentProject.Connection, adOpenForwardOnly, adLockReadOnly
 
  If Not rs.BOF And Not rs.EOF Then
    rs.MoveFirst
    dTotalHoursWorked = rs!CalcDailyHours
  End If
 
  MsgBox "This employee (" & empID & ") has worked: " & dTotalHoursWorked & " this week"
 
 
  ' You can set your field value here
  ' Me.emp1twh = dTotalHoursWorked
 
  Set rs = Nothing

End Sub


Alan :)
0
 
LVL 26

Expert Comment

by:Alan Warren
ID: 9821317
Hi Jessica,

You can drop this line I was using it for testing, I created a temp field in the table to calculate hours on a daily basis:

 Me.emp1tdh = (DateDiff("n", [emp1ti], [emp1to]) / 60) - 0.5



Alan
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now