|
Example Application: A Time Sheet |
|
|
A time sheet is a dialog box or a form on which
employees record the time they have worked. Most companies use a two-week
schedule. In most cases, an employee records the time one week at a time.
|
If you create an application for a time sheet, of
course there are various ways you can implement it, depending on what the
company or its accounting department wants. For our simple example, we
will first let a user enter his employee number and the starting date of
the time sheet. Based on these two pieces of information, we will check
whether the employee had previously entered or started a time sheet for
that time frame. If that’s the case, we will open that time sheet and
display its record. If there is no time recorded for the employee in that
time frame, we will let the user create a new time sheet.
Obviously the primary users of a time sheet are the
employees. A company may need a lot of information from employees but in
most cases, an employee number and the name of the employee are enough.
Practical
Learning: Introducing a Time Sheet
|
|
- Start Microsoft Access
- In the File Name section, change the database name to
TimeSheetCalculations
- Click Create
- On the Ribbon, click Create
- In the Tables section, click Table Design
- Create the columns as follows:
Field Name |
Data Type |
Field Size |
Format |
Caption |
EmployeeNumber |
Short Text |
10 |
|
Employee # |
FirstName |
Short Text |
25 |
|
First Name |
LastName |
Short Text |
25 |
|
Last Name |
HourlySalary |
Number |
Double |
Fixed |
Hourly Salary |
- In the top section, right-click EmployeeNumber and click Primary
Key
- Close the table
- When asked whether you want to save, click Yes
- Set the name to Employees and click OK
As far as a database is concerned, a time sheet is a
list that contains some information about the employees and the time they
workd. In most cases, the basic information about an employee is just what
is enough to identify the employee. This should be a piece of informatiion
that uniquely identify each emmployee. For most businesses, this is done
through an employee number. The time worked is registered for each day
from 0 to 24. Most companies also allow fractions of an hour, usually as
half an hour. Some (rare) companies also consider time in fractions of
quarters (.25,.50, and .75).
After an employee has filled her time sheet, she can
click a button such as Submit, OK, or Apply. In Microsoft Access, a simple
way to create a time record would consist of calling the AddNew()
method of a recordset, assigning the desired values to the fields on the
table, and closing this by calling the Update() method of the
recordset. Here is an example:
Private Sub cmdSubmit_Click()
On Error GoTo cmdSubmitClick_Error
Dim dbFunDS As Database
Dim rsTimeSheets As Recordset
If IsNull(txtEmployeeNumber) Or IsEmpty(txtEmployeeNumber) Then
Exit Sub
End If
If IsNull(txtStartDate) Or IsEmpty(txtStartDate) Then
Exit Sub
End If
Set dbFunDS = CurrentDb
Set rsTimeSheets = dbFunDS.OpenRecordset("TimeSheets1")
With rsTimeSheets
.AddNew
.Fields("EmployeeNumber").Value = txtEmployeeNumber
.Fields("StartDate").Value = txtStartDate
.Fields("Week1Monday").Value = txtWeek1Monday
.Fields("Week1Tuesday").Value = txtWeek1Tuesday
.Fields("Week1Wednesday").Value = txtWeek1Wednesday
.Fields("Week1Thursday").Value = txtWeek1Thursday
.Fields("Week1Friday").Value = txtWeek1Friday
.Fields("Week1Saturday").Value = txtWeek1Saturday
.Fields("Week1Sunday").Value = txtWeek1Sunday
.Fields("Week2Monday").Value = txtWeek2Monday
.Fields("Week2Tuesday").Value = txtWeek2Tuesday
.Fields("Week2Wednesday").Value = txtWeek2Wednesday
.Fields("Week2Thursday").Value = txtWeek2Thursday
.Fields("Week2Friday").Value = txtWeek2Friday
.Fields("Week2Saturday").Value = txtWeek2Saturday
.Fields("Week2Sunday").Value = txtWeek2Sunday
.Update
End With
Set rsTimeSheets = Nothing
Set dbFunDS = Nothing
Exit Sub
cmdSubmitClick_Error:
If Err.Number = 3021 Then
MsgBox "Invalid operation: A problem occurred when trying to submit the time sheet." & vbCrLf & _
"Error #: " & Err.Number & vbCrLf & _
vbOKOnly Or vbInformation, "Fun Department Store"
txtEmployeeName = ""
Exit Sub
Else
End If
Resume Next
End Sub
This code is fine if the employee is only creating a
new time sheet. Most companies require that each employee enter the time
worked at the end of the day, and most of the time, the employee registers
the time worked for one day at a time, or for only that day. To address
this issue, after an employee has entered his employee number and the
start date of the time sheet, we will check if the employee had previously
entered time for at least one day based on the employee number and the
start date. If we find such a record, we will open that time sheet and let
the user update it. If we don't find a time sheet with that information,
we will consider that the employee has filling a new time sheet.
Practical
Learning: Introducing a Time Sheet
|
|
- On the Ribbon, click Create
- In the Forms section, click Form Design
- Design a form as follows:
|
Control |
Text Box Name |
Caption |
Text Box |
|
EmployeeNumber |
Employee #: |
Text Box |
|
txtEmployeeName |
|
Text Box |
|
txtStartDate |
Start Date: |
Text Box |
|
txtEndDate |
End Date: |
Label |
|
|
Monday |
Label |
|
|
Tuesday |
Label |
|
|
Wednesday |
Label |
|
|
Thursday |
Label |
|
|
Friday |
Label |
|
|
Saturday |
Label |
|
|
Sunday |
Text Box |
|
txtWeek1Monday |
Week 1: |
Text Box |
|
txtWeek1Tuesday |
|
Text Box |
|
txtWeek1Wednesday |
|
Text Box |
|
txtWeek1Thursday |
|
Text Box |
|
txtWeek1Friday |
|
Text Box |
|
txtWeek1Saturday |
|
Text Box |
|
txtWeek1Sunday |
|
Text Box |
|
txtWeek2Monday |
Week 2: |
Text Box |
|
txtWeek2Tuesday |
|
Text Box |
|
txtWeek2Wednesday |
|
Text Box |
|
txtWeek2Thursday |
|
Text Box |
|
txtWeek2Friday |
|
Text Box |
|
txtWeek2Saturday |
|
Text Box |
|
txtWeek2Sunday |
|
Button |
|
cmdSubmit |
Submit |
Button |
|
cmdClose |
Close |
|
- Save the form as NewTimeSheet
- Right-click the Submit button and click Build Event...
- In the Choose Builder dialog box, double-click Code Builder
- Set the code in the module as follows:
Option Compare Database
Option Explicit
Private Sub ResetForm()
txtTimeSheetID.Visible = False
txtEmployeeNumber = ""
txtEmployeeName = ""
txtStartDate = ""
txtEndDate = ""
txtWeek1Monday = "0.00"
txtWeek1Tuesday = "0.00"
txtWeek1Wednesday = "0.00"
txtWeek1Thursday = "0.00"
txtWeek1Friday = "0.00"
txtWeek1Saturday = "0.00"
txtWeek1Sunday = "0.00"
txtWeek2Monday = "0.00"
txtWeek2Tuesday = "0.00"
txtWeek2Wednesday = "0.00"
txtWeek2Thursday = "0.00"
txtWeek2Friday = "0.00"
txtWeek2Saturday = "0.00"
txtWeek2Sunday = "0.00"
End Sub
Private Sub Form_Load()
ResetForm
End Sub
Private Sub txtEmployeeNumber_LostFocus()
On Error GoTo txtEmployeeNumber_Error
Dim dbFunDS As Database
Dim rsEmployees As Recordset
Dim EmployeeFound As Boolean
If IsNull(txtEmployeeNumber) Or IsEmpty(txtEmployeeNumber) Or (txtEmployeeNumber = "") Then
txtEmployeeName = ""
Exit Sub
Else
Set dbFunDS = CurrentDb
Set rsEmployees = dbFunDS.OpenRecordset("SELECT FirstName, LastName " & _
"FROM Employees " & _
"WHERE EmployeeNumber = '" & txtEmployeeNumber & "';")
If rsEmployees.RecordCount > 0 Then
EmployeeFound = True
txtEmployeeName = rsEmployees("LastName") & ", " & rsEmployees("FirstName")
End If
If EmployeeFound = False Then
MsgBox "There is no staff member with that employee number.", _
vbOKOnly Or vbInformation, _
"Fun Department Store - Employee Time Sheet"
End If
Set dbFunDS = Nothing
Set rsEmployees = Nothing
End If
Exit Sub
txtEmployeeNumber_Error:
If Err.Number = 3021 Then
MsgBox "Invalid Employee Number: The employee number you entered was not found in the database.", _
vbOKOnly Or vbInformation, "Fun Department Store - Employee Time Sheet"
Exit Sub
Else
MsgBox "A problem occurred when trying to retrieve the employee record." & vbCrLf & _
"Error #: " & Err.Number & vbCrLf & _
"Description: " & Err.Description, _
vbOKOnly Or vbInformation, "Fun Department Store - Employee Time Sheet"
End If
Resume Next
End Sub
Private Sub txtStartDate_LostFocus()
On Error GoTo txtStartDate_Error
Dim dbFunDS As Database
Dim rsTimeSheets As Recordset
If IsNull(txtEmployeeNumber) Or IsEmpty(txtEmployeeNumber) Then
Exit Sub
ElseIf IsNull(txtStartDate) Or IsEmpty(txtStartDate) Then
Exit Sub
Else
txtEndDate = DateAdd("d", 13, CDate(txtStartDate))
Set dbFunDS = CurrentDb
' get the values in the TimeSheet table
Set rsTimeSheets = dbFunDS.OpenRecordset("SELECT TimeSheetID, EmployeeNumber, StartDate, " & _
"Week1Monday, Week1Tuesday, Week1Wednesday, Week1Thursday, Week1Friday, Week1Saturday, Week1Sunday, " & _
"Week2Monday, Week2Tuesday, Week2Wednesday, Week2Thursday, Week2Friday, Week2Saturday, Week2Sunday " & _
"FROM TimeSheets1 " & _
"WHERE EmployeeNumber = '" & txtEmployeeNumber & "' AND StartDate = #" & txtStartDate & "#;")
' To get the ending date of hte time frame, add 2 weeks to the specified starting date
txtEndDate = DateAdd("d", 13, CDate(txtStartDate))
' Find out if there was a record in the TimeSheet table that corresponds to the specified employee number and the starting date
If rsTimeSheets.RecordCount > 0 Then
' If there exists a time record for the employee
' number and the start date, display its values
txtTimeSheetID.Visible = True
txtTimeSheetID = rsTimeSheets("TimeSheetID")
txtEmployeeNumber = rsTimeSheets("EmployeeNumber")
txtStartDate = rsTimeSheets("StartDate")
txtWeek1Monday = rsTimeSheets("Week1Monday")
txtWeek1Tuesday = rsTimeSheets("Week1Tuesday")
txtWeek1Wednesday = rsTimeSheets("Week1Wednesday")
txtWeek1Thursday = rsTimeSheets("Week1Thursday")
txtWeek1Friday = rsTimeSheets("Week1Friday")
txtWeek1Saturday = rsTimeSheets("Week1Saturday")
txtWeek1Sunday = rsTimeSheets("Week1Sunday")
txtWeek2Monday = rsTimeSheets("Week2Monday")
txtWeek2Tuesday = rsTimeSheets("Week2Tuesday")
txtWeek2Wednesday = rsTimeSheets("Week2Wednesday")
txtWeek2Thursday = rsTimeSheets("Week2Thursday")
txtWeek2Friday = rsTimeSheets("Week2Friday")
txtWeek2Saturday = rsTimeSheets("Week2Saturday")
txtWeek2Sunday = rsTimeSheets("Week2Sunday")
Else
' If no record was found for the employee number in the
' specified time frame, get ready to create a new record
txtTimeSheetID.Visible = False
txtWeek1Monday = "0.00"
txtWeek1Tuesday = "0.00"
txtWeek1Wednesday = "0.00"
txtWeek1Thursday = "0.00"
txtWeek1Friday = "0.00"
txtWeek1Saturday = "0.00"
txtWeek1Sunday = "0.00"
txtWeek2Monday = "0.00"
txtWeek2Tuesday = "0.00"
txtWeek2Wednesday = "0.00"
txtWeek2Thursday = "0.00"
txtWeek2Friday = "0.00"
txtWeek2Saturday = "0.00"
txtWeek2Sunday = "0.00"
End If
End If
Exit Sub
txtStartDate_Error:
If Err.Number = 3021 Then
MsgBox "Invalid start date: The start date you specified is not correct.", _
vbOKOnly Or vbInformation, "Fun Department Store - Employee Time Sheet"
txtEmployeeName = ""
Exit Sub
Else
MsgBox "A problem occurred when trying to retrieve the time sheet." & vbCrLf & _
"Error #: " & Err.Number & vbCrLf & _
"Description: " & Err.Description, _
vbOKOnly Or vbInformation, _
"Fun Department Store - Employee Time Sheet"
End If
Resume Next
End Sub
Private Sub cmdSubmit_Click()
On Error GoTo cmdSubmitClick_Error
Dim dbFunDS As Database
Dim rsTimeSheets As Recordset
If IsNull(txtEmployeeNumber) Or IsEmpty(txtEmployeeNumber) Then
Exit Sub
End If
If IsNull(txtStartDate) Or IsEmpty(txtStartDate) Then
Exit Sub
End If
Set dbFunDS = CurrentDb
' We need to find out whether the user is creating a new time record or he is updating an existing time sheet
Set rsTimeSheets = dbFunDS.OpenRecordset("SELECT TimeSheetID, EmployeeNumber, StartDate, " & _
"Week1Monday, Week1Tuesday, Week1Wednesday, Week1Thursday, Week1Friday, Week1Saturday, Week1Sunday, " & _
"Week2Monday, Week2Tuesday, Week2Wednesday, Week2Thursday, Week2Friday, Week2Saturday, Week2Sunday " & _
"FROM TimeSheets1 " & _
"WHERE EmployeeNumber = '" & txtEmployeeNumber & "' AND StartDate = #" & txtStartDate & "#;")
With rsTimeSheets
If .RecordCount > 0 Then
' If a record was found with the current employee number
' and the specified start date, the employee probably simply wants to update her time sheet
.Edit
.Fields("Week1Monday").Value = txtWeek1Monday
.Fields("Week1Tuesday").Value = txtWeek1Tuesday
.Fields("Week1Wednesday").Value = txtWeek1Wednesday
.Fields("Week1Thursday").Value = txtWeek1Thursday
.Fields("Week1Friday").Value = txtWeek1Friday
.Fields("Week1Saturday").Value = txtWeek1Saturday
.Fields("Week1Sunday").Value = txtWeek1Sunday
.Fields("Week2Monday").Value = txtWeek2Monday
.Fields("Week2Tuesday").Value = txtWeek2Tuesday
.Fields("Week2Wednesday").Value = txtWeek2Wednesday
.Fields("Week2Thursday").Value = txtWeek2Thursday
.Fields("Week2Friday").Value = txtWeek2Friday
.Fields("Week2Saturday").Value = txtWeek2Saturday
.Fields("Week2Sunday").Value = txtWeek2Sunday
.Update
MsgBox "The time sheet has been updated." & vbCrLf & _
vbOKOnly Or vbInformation, "Fun Department Store"
Else
' If no record was found with the current employee number
' and the specified start date, the employee probably wants to create a new time sheet
.AddNew
.Fields("EmployeeNumber").Value = txtEmployeeNumber
.Fields("StartDate").Value = txtStartDate
.Fields("Week1Monday").Value = txtWeek1Monday
.Fields("Week1Tuesday").Value = txtWeek1Tuesday
.Fields("Week1Wednesday").Value = txtWeek1Wednesday
.Fields("Week1Thursday").Value = txtWeek1Thursday
.Fields("Week1Friday").Value = txtWeek1Friday
.Fields("Week1Saturday").Value = txtWeek1Saturday
.Fields("Week1Sunday").Value = txtWeek1Sunday
.Fields("Week2Monday").Value = txtWeek2Monday
.Fields("Week2Tuesday").Value = txtWeek2Tuesday
.Fields("Week2Wednesday").Value = txtWeek2Wednesday
.Fields("Week2Thursday").Value = txtWeek2Thursday
.Fields("Week2Friday").Value = txtWeek2Friday
.Fields("Week2Saturday").Value = txtWeek2Saturday
.Fields("Week2Sunday").Value = txtWeek2Sunday
.Update
MsgBox "The new time sheet has been created and saved." & vbCrLf & _
vbOKOnly Or vbInformation, "Fun Department Store"
End If
End With
Set rsTimeSheets = Nothing
Set dbFunDS = Nothing
ResetForm
Exit Sub
cmdSubmitClick_Error:
If Err.Number = 3021 Then
MsgBox "Invalid operation: A problem occurred when trying to submit the time sheet." & vbCrLf & _
"Error #: " & Err.Number & vbCrLf & _
vbOKOnly Or vbInformation, _
"Fun Department Store - Employee Time Sheet"
ResetForm
Exit Sub
Else
End If
Resume Next
End Sub
Private Sub cmdClose_Click()
On Error GoTo cmdClose_Click_Err
DoCmd.Close , ""
cmdClose_Click_Exit:
Exit Sub
cmdClose_Click_Err:
MsgBox Error$
Resume cmdClose_Click_Exit
End Sub
Private Sub txtStartDate_BeforeUpdate(Cancel As Integer)
End Sub
- On the Ribbon, click Create
- In the Queries section, click Query Design
- On the Show Table dialog box, click Close
- Right-click the top section of the view and click SQL View
- Type the following code:
CREATE TABLE TimeSheets
(
TimeSheetID Counter(1000, 1),
EmployeeNumber Text(10) not null
);
- To execute, in the Results section of the Design tab of the
Ribbon, click Run
- Close the Query1 window
- When asked whether you want to save, click No
- In the Navigation Pane, right-click TimeSheets and click Design
View
- Complete the design of the table as follows:
Field Name |
Data Type |
Field Size |
Format |
Caption |
Default Value |
TimeSheetID |
No Change |
No Change |
|
Time Sheet ID |
|
EmployeeNumber |
Short Text |
10 |
|
Employee # |
|
StartDate |
Date/Time |
|
Long Date |
Start Date |
|
Week1Monday |
Number |
Double |
Fixed |
Week 1 Monday |
0.00 |
Week1Tuesday |
Number |
Double |
Fixed |
Week 1 Tuesday |
0.00 |
Week1Wednesday |
Number |
Double |
Fixed |
Week 1 Wednesday |
0.00 |
Week1Thursday |
Number |
Double |
Fixed |
Week 1 Thursday |
0.00 |
Week1Friday |
Number |
Double |
Fixed |
Week 1 Friday |
0.00 |
Week1Saturday |
Number |
Double |
Fixed |
Week 1 Saturday |
0.00 |
Week1Sunday |
Number |
Double |
Fixed |
Week 1 Sunday |
0.00 |
Week2Monday |
Number |
Double |
Fixed |
Week 2 Monday |
0.00 |
Week2Tuesday |
Number |
Double |
Fixed |
Week 2 Tuesday |
0.00 |
Week2Wednesday |
Number |
Double |
Fixed |
Week 2 Wednesday |
0.00 |
Week2Thursday |
Number |
Double |
Fixed |
Week 2 Thursday |
0.00 |
Week2Friday |
Number |
Double |
Fixed |
Week 2 Friday |
0.00 |
Week2Saturday |
Number |
Double |
Fixed |
Week 2 Saturday |
0.00 |
Week2Sunday |
Number |
Double |
Fixed |
Week 2 Sunday |
0.00 |
- In the top section, right-click TimeSheetID and click Primary Key
- Close the table
- When asked whether you want to save, click Yes
- Set the name to TimeSheets and click OK
Overtime and the Weekly Time Sheet
|
|
The easiest way to calculate the values of a time
sheet is to simply add the time worked for each day to get the total time
worked. If an employee worked some limited time, this is fine. Most
companies pay overtime if an employee works beyond a certain amount of
time. There are different techniques to apply or to calculate overtime,
but there are two broad categories.
The time worked can be considered on a weekly basis.
In this case, an employee typically works 5 days a week for 40 hours. Any
period over 40 hours is considered overtime.
Practical
Learning: Evaluating a Payroll
|
|
- On the Ribbon, click Create
- In the Forms section, click Form Design
- Save the form as PayrollEvaluation1
- Design a form as follows:
|
Control |
Text Box Name |
Caption |
Other Properties |
Text Box |
|
txtStartDate |
Start Date: |
|
Text Box |
|
txtEndDate |
End Date: |
|
Text Box |
|
EmployeeNumber |
Employee #: |
|
Text Box |
|
txtEmployeeName |
|
|
Button |
|
cmdEvaluate |
Evaluate |
|
Label |
|
txtHourlySalary |
Hourly Salary: |
|
Label |
|
txtTimeSheetID |
Time Sheet ID: |
Visible: No |
Line |
|
|
|
Border Width: 3 Pt Border Color: Text 1,
Lighter 50% |
Label |
|
|
Monday |
|
Label |
|
|
Tuesday |
|
Label |
|
|
Wednesday |
|
Label |
|
|
Thursday |
|
Label |
|
|
Friday |
|
Label |
|
|
Saturday |
|
Label |
|
|
Sunday |
|
Label |
|
|
Total |
|
Line |
|
|
|
|
Text Box |
|
txtWeek1Monday |
Week 1: |
|
Text Box |
|
txtWeek1Tuesday |
|
|
Text Box |
|
txtWeek1Wednesday |
|
|
Text Box |
|
txtWeek1Thursday |
|
|
Text Box |
|
txtWeek1Friday |
|
|
Text Box |
|
txtWeek1Saturday |
|
|
Text Box |
|
txtWeek1Sunday |
|
|
Text Box |
|
txtTotalTimeWeek1 |
|
|
Line |
|
|
|
|
Text Box |
|
txtWeek2Monday |
Week 2: |
|
Text Box |
|
txtWeek2Tuesday |
|
|
Text Box |
|
txtWeek2Wednesday |
|
|
Text Box |
|
txtWeek2Thursday |
|
|
Text Box |
|
txtWeek2Friday |
|
|
Text Box |
|
txtWeek2Saturday |
|
|
Text Box |
|
txtWeek2Sunday |
|
|
Text Box |
|
txtTotalTimeWeek2 |
|
|
Line |
|
|
|
Border Width: 3 Pt Border Color: Text 1,
Lighter 50% |
Label |
|
|
Time |
|
Label |
|
|
Pay |
|
Text Box |
|
txtRegularTime |
Regular: |
|
Text Box |
|
txtRegularPay |
|
|
Text Box |
|
txtOvertime |
Overtime: |
|
Text Box |
|
txtOvertimePay |
|
|
Line |
|
|
|
|
Text Box |
|
txtGrossSalary |
Gross Salary: |
|
Button |
|
cmdClose |
Close |
|
|
- Right-click the Evaluate button and click Build Event...
- In the Choose Builder dialog box, double-click Code Builder
- Set the code in the module as follows:
Option Compare Database
Option Explicit
Private Sub txtStartDate_LostFocus()
If IsNull(txtStartDate) Or IsEmpty(txtStartDate) Then
txtEndDate = ""
Exit Sub
End If
txtEndDate = DateAdd("d", 13, CDate(txtStartDate))
End Sub
Private Sub txtEmployeeNumber_LostFocus()
On Error GoTo txtEmployeeNumber_Error
Dim dbFunDS As Database
Dim rsEmployees As Recordset
If IsNull(txtEmployeeNumber) Or IsEmpty(txtEmployeeNumber) Then
txtEmployeeName = ""
txtHourlySalary = ""
Exit Sub
End If
Set dbFunDS = CurrentDb
Set rsEmployees = dbFunDS.OpenRecordset("SELECT FirstName, LastName, HourlySalary " & _
"FROM Employees " & _
"WHERE EmployeeNumber = '" & txtEmployeeNumber & "';")
txtEmployeeName = rsEmployees("LastName") & ", " & rsEmployees("FirstName")
txtHourlySalary = rsEmployees("HourlySalary")
Set dbFunDS = Nothing
Set rsEmployees = Nothing
Exit Sub
txtEmployeeNumber_Error:
If Err.Number = 3021 Then
MsgBox "Invalid Employee Number: The employee number you entered was not found in the database.", _
vbOKOnly Or vbInformation, "Fun Department Store - Employee Time Sheet"
Exit Sub
Else
MsgBox "A problem occurred when trying to retrieve the employee record." & vbCrLf & _
"Error #: " & Err.Number & vbCrLf & _
"Description: " & Err.Description, _
vbOKOnly Or vbInformation, _
"Fun Department Store - Employee Payroll"
End If
Resume Next
End Sub
Private Sub cmdEvaluate_Click()
Dim dbFunDS As Database
Dim rsTimeSheets As Recordset
Dim HourlySalary As Double
Dim TotalTimeWeek1 As Double
Dim TotalTimeWeek2 As Double
Dim OvertimeSalary As Double
Dim RegularTimeWeek1 As Double
Dim RegularPayWeek1 As Double
Dim OvertimeWeek1 As Double
Dim OvertimePayWeek1 As Double
Dim RegularTimeWeek2 As Double
Dim RegularPayWeek2 As Double
Dim OvertimeWeek2 As Double
Dim OvertimePayWeek2 As Double
Dim TotalRegularTime As Double
Dim TotalOvertime As Double
Dim TotalRegularPay As Double
Dim OvertimePay As Double
Dim TotalEarnings As Double
Dim TimeSheetFound As Boolean
If IsNull(txtStartDate) Or IsEmpty(txtStartDate) Then
Exit Sub
End If
If IsNull(txtEmployeeNumber) Or IsEmpty(txtEmployeeNumber) Then
Exit Sub
End If
HourlySalary = 0#
TotalTimeWeek1 = 0#
TotalTimeWeek2 = 0#
RegularTimeWeek1 = 0#
RegularPayWeek1 = 0#
OvertimeWeek1 = 0#
OvertimePayWeek1 = 0#
RegularTimeWeek2 = 0#
RegularPayWeek2 = 0#
OvertimeWeek2 = 0#
OvertimePayWeek2 = 0#
TotalRegularTime = 0#
TotalOvertime = 0#
TotalRegularPay = 0#
OvertimePay = 0#
TotalEarnings = 0#
TimeSheetFound = False
Set dbFunDS = CurrentDb
' Get the values in the TimeSheet table
Set rsTimeSheets = dbFunDS.OpenRecordset("SELECT TimeSheetID, EmployeeNumber, StartDate, " & _
"Week1Monday, Week1Tuesday, Week1Wednesday, Week1Thursday, Week1Friday, Week1Saturday, Week1Sunday, " & _
"Week2Monday, Week2Tuesday, Week2Wednesday, Week2Thursday, Week2Friday, Week2Saturday, Week2Sunday " & _
"FROM TimeSheets1 " & _
"WHERE EmployeeNumber = '" & txtEmployeeNumber & "' AND StartDate = #" & txtStartDate & "#;")
' Find out if there was a record in the TimeSheet table that corresponds to the specified employee number and the starting date
If rsTimeSheets.RecordCount > 0 Then
' If there exists a time record for the employee
' number and the start date, display its values
txtTimeSheetID.Visible = True
txtTimeSheetID = rsTimeSheets("TimeSheetID")
txtEmployeeNumber = rsTimeSheets("EmployeeNumber")
txtStartDate = rsTimeSheets("StartDate")
txtWeek1Monday = rsTimeSheets("Week1Monday")
txtWeek1Tuesday = rsTimeSheets("Week1Tuesday")
txtWeek1Wednesday = rsTimeSheets("Week1Wednesday")
txtWeek1Thursday = rsTimeSheets("Week1Thursday")
txtWeek1Friday = rsTimeSheets("Week1Friday")
txtWeek1Saturday = rsTimeSheets("Week1Saturday")
txtWeek1Sunday = rsTimeSheets("Week1Sunday")
txtWeek2Monday = rsTimeSheets("Week2Monday")
txtWeek2Tuesday = rsTimeSheets("Week2Tuesday")
txtWeek2Wednesday = rsTimeSheets("Week2Wednesday")
txtWeek2Thursday = rsTimeSheets("Week2Thursday")
txtWeek2Friday = rsTimeSheets("Week2Friday")
txtWeek2Saturday = rsTimeSheets("Week2Saturday")
txtWeek2Sunday = rsTimeSheets("Week2Sunday")
TimeSheetFound = True
TotalTimeWeek1 = CDbl(txtWeek1Monday) + CDbl(txtWeek1Tuesday) + CDbl(txtWeek1Wednesday) + CDbl(txtWeek1Thursday) + CDbl(txtWeek1Friday) + CDbl(txtWeek1Saturday) + CDbl(txtWeek1Sunday)
TotalTimeWeek2 = CDbl(txtWeek2Monday) + CDbl(txtWeek2Tuesday) + CDbl(txtWeek2Wednesday) + CDbl(txtWeek2Thursday) + CDbl(txtWeek2Friday) + CDbl(txtWeek2Saturday) + CDbl(txtWeek2Sunday)
HourlySalary = CDbl(txtHourlySalary)
Rem The overtime is paid time and half
OvertimeSalary = HourlySalary * 1.5
Rem If the employee worked under 40 hours, there is no overtime
If TotalTimeWeek1 < 40# Then
RegularTimeWeek1 = TotalTimeWeek1
RegularPayWeek1 = HourlySalary * RegularTimeWeek1
OvertimeWeek1 = 0#
OvertimePayWeek1 = 0#
Rem If the employee worked over 40 hours, calculate the overtime
ElseIf TotalTimeWeek1 >= 40# Then
RegularTimeWeek1 = 40#
RegularPayWeek1 = HourlySalary * 40#
OvertimeWeek1 = TotalTimeWeek1 - 40#
OvertimePayWeek1 = OvertimeWeek1 * OvertimeSalary
End If
If TotalTimeWeek2 < 40# Then
RegularTimeWeek2 = TotalTimeWeek2
RegularPayWeek2 = HourlySalary * RegularTimeWeek2
OvertimeWeek2 = 0#
OvertimePayWeek2 = 0#
ElseIf TotalTimeWeek2 >= 40# Then
RegularTimeWeek2 = 40#
RegularPayWeek2 = HourlySalary * 40#
OvertimeWeek2 = TotalTimeWeek2 - 40#
OvertimePayWeek2 = OvertimeWeek2 * OvertimeSalary
End If
txtTotalTimeWeek1 = FormatNumber(TotalTimeWeek1)
txtTotalTimeWeek2 = FormatNumber(TotalTimeWeek2)
TotalRegularTime = RegularTimeWeek1 + RegularTimeWeek2
TotalOvertime = OvertimeWeek1 + OvertimeWeek2
TotalRegularPay = RegularPayWeek1 + RegularPayWeek2
OvertimePay = OvertimePayWeek1 + OvertimePayWeek2
TotalEarnings = TotalRegularPay + OvertimePay
txtRegularTime = FormatNumber(TotalRegularTime)
txtOvertime = FormatNumber(TotalOvertime)
txtRegularPay = FormatNumber(TotalRegularPay)
txtOvertimePay = FormatNumber(OvertimePay)
txtGrossSalary = FormatNumber(TotalEarnings)
End If
If TimeSheetFound = False Then
MsgBox "There is no time sheet for that start date and that employee number.", _
vbOKOnly Or vbInformation, _
"FunDS - Employees Payroll"
' If no record was found for the employee number in the
' specified time frame, get ready to create a new record
txtTimeSheetID.Visible = False
txtWeek1Monday = "0.00"
txtWeek1Tuesday = "0.00"
txtWeek1Wednesday = "0.00"
txtWeek1Thursday = "0.00"
txtWeek1Friday = "0.00"
txtWeek1Saturday = "0.00"
txtWeek1Sunday = "0.00"
txtWeek2Monday = "0.00"
txtWeek2Tuesday = "0.00"
txtWeek2Wednesday = "0.00"
txtWeek2Thursday = "0.00"
txtWeek2Friday = "0.00"
txtWeek2Saturday = "0.00"
txtWeek2Sunday = "0.00"
End If
End Sub
Private Sub cmdClose_Click()
On Error GoTo cmdClose_Click_Err
DoCmd.Close , ""
cmdClose_Click_Exit:
Exit Sub
cmdClose_Click_Err:
MsgBox Error$
Resume cmdClose_Click_Exit
End Sub
- Return to Microsoft Access
- Close the form
- When asked whether you want to save, click Yes
|
|