I was recently faced with the challange of generating all the payroll for a particular month for all employees before adding any other deductions and earnings individually. This way the process will be faster when I have a lot of employees on the system rather than processing each payroll one after the other.
The code below do all that with a click of a button.
------------------------ Code begin -----------------------------
Public Function GeneratePayroll()
Dim dbsPayroll As DAO.Database
Dim rstEmployees As DAO.Recordset
Dim rstEmployeesPay As Recordset
Dim strSQL As String
Dim strInput As String, strMsg As String
On Error GoTo ErrorHandler
Set dbsPayroll = CurrentDb
Set rstEmployeesPay = dbsPayroll.OpenRecordset("tblPayroll", dbOpenDynaset)
'Open recordsets on the Employees tables. If there are
'no records in either table, exit the function.
strSQL = "SELECT * FROM tblEmployees ORDER BY EmployeeID;"
Set rstEmployees = dbsPayroll.OpenRecordset(strSQL, dbOpenSnapshot)
If rstEmployees.EOF Then Exit Function
'loop through employees until end of file
Do Until rstEmployees.EOF
'Add this record to the payroll table
rstEmployeesPay.MoveFirst
rstEmployeesPay.AddNew
rstEmployeesPay("EmployeeID") = rstEmployees("EmployeeID")
rstEmployeesPay("PaymentMonth") = Forms!frmPayrollRun!PayMonth
rstEmployeesPay("PaymentDate") = Forms!frmPayrollRun!PayDay
rstEmployeesPay("Paid") = Forms!frmPayrollRun!Paid
rstEmployeesPay("PaymentType") = Forms!frmPayrollRun!PayType
rstEmployeesPay("PaymentMethod") = Forms!frmPayrollRun!PayMethod
rstEmployeesPay("Currency") = Forms!frmPayrollRun!Currency
rstEmployeesPay("CostCentre") = Forms!frmPayrollRun!CostCentre
rstEmployeesPay("BasicSalary") = DLookup("BasicSalary", "tblEmployeesLevel", "JobLevelID = " & rstEmployees("JobLevelID"))
rstEmployeesPay("MealAllowance") = DLookup("MealAllowance", "tblEmployeesLevel", "JobLevelID = " & rstEmployees("JobLevelID"))
rstEmployeesPay("TransportAllowance") = DLookup("TransportAllowance", "tblEmployeesLevel", "JobLevelID = " & rstEmployees("JobLevelID"))
rstEmployeesPay("UtilityAllowance") = DLookup("UtilityAllowance", "tblEmployeesLevel", "JobLevelID = " & rstEmployees("JobLevelID"))
rstEmployeesPay("EntertainmentAllowance") = DLookup("EntertainmentAllowance", "tblEmployeesLevel", "JobLevelID = " & rstEmployees("JobLevelID"))
rstEmployeesPay("HousingAllowance") = DLookup("HousingAllowance", "tblEmployeesLevel", "JobLevelID = " & rstEmployees("JobLevelID"))
rstEmployeesPay("HazardAllowance") = DLookup("HazardAllowance", "tblEmployeesLevel", "JobLevelID = " & rstEmployees("JobLevelID"))
'rstEmployeesPay("Note") = strInput
rstEmployeesPay("Note") = Forms!frmPayrollRun!Notes
rstEmployeesPay.Update
' Move to the next record.
rstEmployees.MoveNext
Loop
'close and clear the recordset from cache
rstEmployees.Close
rstEmployeesPay.Close
dbsPayroll.Close
Set rstEmployees = Nothing
Set rstEmployeesPay = Nothing
Set dbsPayroll = Nothing
'Inform the user that the payroll has been created.
MsgBox "The payroll has been created successfully for all staff."
Exit Function
ErrorHandler:
MsgBox "Error #: " & Err.Number & vbCrLf & vbCrLf & Err.Description
End Function
----------------------------- Code End --------------------------------------------
Once I call the function above, it generates a payslip for all the employees on the employees table with great success.
However, I have another challange or twist to this. I want to be able to also create an additional earning but this time on another table (tblPayrollEarnings) for all the payrollID created above. What I mean is that as the first payroll is created for the first employee from the employees table, a new earning should also be created in the tblPayrollEarnings with this first PayrollID at the click of the same button as the function above.
How can I incorporate the code below into the one above:
--------------------- Code begin -------------------------
Sub PostEarning()
Dim dbEarning As Database
Dim rsEarning As Recordset
Set dbEarning = CurrentDb
Set rsEarning = dbEarning.OpenRecordset("tblPayrollEarnings", DB_OPEN_DYNASET)
rsEarning.AddNew
rsEarning("PayrollID") = rstEmployeesPay("PayrollID")
rsEarning("EarningCategory") = "Overtime"
rsEarning("EarningAmount") = 10000
rsEarning.Update
rsEarning.Close
dbEarning.Close
Set rsEarning = Nothing
Set dbEarning = Nothing
End Sub
----------------------- Code End -------------------------------------
In my immediate code above I used
rsEarning("PayrollID") = rstEmployeesPay("PayrollID")
to capture the new payrollID that I want to post to the tblPayrollEarnings. Does anyone know if that is correct or not. All atempts to combine both codes above have been a nightmare all day. Any help would be appreciated please.
I have attached a stripped down version of my database for anyone that want to assist me.
Many thanks in advance.
The code below do all that with a click of a button.
------------------------ Code begin -----------------------------
Public Function GeneratePayroll()
Dim dbsPayroll As DAO.Database
Dim rstEmployees As DAO.Recordset
Dim rstEmployeesPay As Recordset
Dim strSQL As String
Dim strInput As String, strMsg As String
On Error GoTo ErrorHandler
Set dbsPayroll = CurrentDb
Set rstEmployeesPay = dbsPayroll.OpenRecordset("tblPayroll", dbOpenDynaset)
'Open recordsets on the Employees tables. If there are
'no records in either table, exit the function.
strSQL = "SELECT * FROM tblEmployees ORDER BY EmployeeID;"
Set rstEmployees = dbsPayroll.OpenRecordset(strSQL, dbOpenSnapshot)
If rstEmployees.EOF Then Exit Function
'loop through employees until end of file
Do Until rstEmployees.EOF
'Add this record to the payroll table
rstEmployeesPay.MoveFirst
rstEmployeesPay.AddNew
rstEmployeesPay("EmployeeID") = rstEmployees("EmployeeID")
rstEmployeesPay("PaymentMonth") = Forms!frmPayrollRun!PayMonth
rstEmployeesPay("PaymentDate") = Forms!frmPayrollRun!PayDay
rstEmployeesPay("Paid") = Forms!frmPayrollRun!Paid
rstEmployeesPay("PaymentType") = Forms!frmPayrollRun!PayType
rstEmployeesPay("PaymentMethod") = Forms!frmPayrollRun!PayMethod
rstEmployeesPay("Currency") = Forms!frmPayrollRun!Currency
rstEmployeesPay("CostCentre") = Forms!frmPayrollRun!CostCentre
rstEmployeesPay("BasicSalary") = DLookup("BasicSalary", "tblEmployeesLevel", "JobLevelID = " & rstEmployees("JobLevelID"))
rstEmployeesPay("MealAllowance") = DLookup("MealAllowance", "tblEmployeesLevel", "JobLevelID = " & rstEmployees("JobLevelID"))
rstEmployeesPay("TransportAllowance") = DLookup("TransportAllowance", "tblEmployeesLevel", "JobLevelID = " & rstEmployees("JobLevelID"))
rstEmployeesPay("UtilityAllowance") = DLookup("UtilityAllowance", "tblEmployeesLevel", "JobLevelID = " & rstEmployees("JobLevelID"))
rstEmployeesPay("EntertainmentAllowance") = DLookup("EntertainmentAllowance", "tblEmployeesLevel", "JobLevelID = " & rstEmployees("JobLevelID"))
rstEmployeesPay("HousingAllowance") = DLookup("HousingAllowance", "tblEmployeesLevel", "JobLevelID = " & rstEmployees("JobLevelID"))
rstEmployeesPay("HazardAllowance") = DLookup("HazardAllowance", "tblEmployeesLevel", "JobLevelID = " & rstEmployees("JobLevelID"))
'rstEmployeesPay("Note") = strInput
rstEmployeesPay("Note") = Forms!frmPayrollRun!Notes
rstEmployeesPay.Update
' Move to the next record.
rstEmployees.MoveNext
Loop
'close and clear the recordset from cache
rstEmployees.Close
rstEmployeesPay.Close
dbsPayroll.Close
Set rstEmployees = Nothing
Set rstEmployeesPay = Nothing
Set dbsPayroll = Nothing
'Inform the user that the payroll has been created.
MsgBox "The payroll has been created successfully for all staff."
Exit Function
ErrorHandler:
MsgBox "Error #: " & Err.Number & vbCrLf & vbCrLf & Err.Description
End Function
----------------------------- Code End --------------------------------------------
Once I call the function above, it generates a payslip for all the employees on the employees table with great success.
However, I have another challange or twist to this. I want to be able to also create an additional earning but this time on another table (tblPayrollEarnings) for all the payrollID created above. What I mean is that as the first payroll is created for the first employee from the employees table, a new earning should also be created in the tblPayrollEarnings with this first PayrollID at the click of the same button as the function above.
How can I incorporate the code below into the one above:
--------------------- Code begin -------------------------
Sub PostEarning()
Dim dbEarning As Database
Dim rsEarning As Recordset
Set dbEarning = CurrentDb
Set rsEarning = dbEarning.OpenRecordset("tblPayrollEarnings", DB_OPEN_DYNASET)
rsEarning.AddNew
rsEarning("PayrollID") = rstEmployeesPay("PayrollID")
rsEarning("EarningCategory") = "Overtime"
rsEarning("EarningAmount") = 10000
rsEarning.Update
rsEarning.Close
dbEarning.Close
Set rsEarning = Nothing
Set dbEarning = Nothing
End Sub
----------------------- Code End -------------------------------------
In my immediate code above I used
rsEarning("PayrollID") = rstEmployeesPay("PayrollID")
to capture the new payrollID that I want to post to the tblPayrollEarnings. Does anyone know if that is correct or not. All atempts to combine both codes above have been a nightmare all day. Any help would be appreciated please.
I have attached a stripped down version of my database for anyone that want to assist me.
Many thanks in advance.