Tom:
This is close to what I'm talking about, but not quite there. Here's my
problem:
The form (named "Employee_Main") containing the command button that
calculates the deductions refers to a function that defines the current
record by the value in two drop-down lists using a SQL command from another
form named "Employee_Home". The drop-down lists run macros; the first list
contains our four locations and runs a query for all records for the location
selected, the second list contains the Employee IDs for the location
selected. The user would then select the employee they wish to edit from the
drop-down list and click on a command button to edit the record. Here's my
code:
In the Employee_Home form the following applies to open the form so the user
can edit the employee record (which contains the command button to update
their deductions):
Private Sub Update_Employee_Button_Click()
On Error GoTo Update_Employee_Button_Click_Err
'MsgBox "Selected value is " & testchooseemp
DoCmd.SetWarnings WarningsOff
strSQLArchive = "INSERT INTO [Employee_Archive]" & _
"SELECT [Employee_Main].* FROM [Employee_Main]" & _
"WHERE (([Employee_Main].[Emp_ID]) = Chooseemp);"
DoCmd.RunSQL (strSQLArchive)
DoCmd.OpenForm "Employee_Main", acNormal, , "([Employee_Main].[Emp_ID])
= Forms!Employee_Home!Chooseemp", acFormEdit
Forms![Employee_Main]![ChooseEmp] = Forms!Employee_Home!ChooseEmp
Update_Employee_Button_Click_Exit:
Exit Sub
Update_Employee_Button_Click_Err:
MsgBox Error$
Resume Update_Employee_Button_Click_Exit
End Sub
In the form Employee_Main, the user then clicks on the command button to
calculate deductions, which contains the following code:
Public Sub Calculate_Deductions_Click()
On Error GoTo Calculate_Deductions_Click_Err
Last_Upd_Dt = Date
'Me.SaveRecord
DoCmd.RunCommand (acCmdSaveRecord)
DoCmd.SetWarnings Off
strSQL_Last_Upd_Dt = "UPDATE Employee_Main " _
& "SET Employee_Main.Last_Upd_Dt = Date() " _
& "WHERE (([Employee_Main].[Emp_ID]) = Chooseemp) ;"
DoCmd.SetWarnings Off
strSQL_Birth_Month = "UPDATE Employee_Main " _
& "SET Employee_Main.Birth_Month = Month([Employee_Main.DOB]) " _
& "WHERE (([Employee_Main].[Emp_ID]) = Chooseemp) ;"
DoCmd.RunSQL (strSQL_Birth_Month)
strSQL_Birth_Day = "UPDATE Employee_Main " _
& "SET Employee_Main.Birth_Day = Day([Employee_Main.DOB]) " _
& "WHERE (([Employee_Main].[Emp_ID]) = Chooseemp) ;"
DoCmd.RunSQL (strSQL_Birth_Day)
strSQL_Birth_Year = "UPDATE Employee_Main " _
& "SET Employee_Main.Birth_Year = Year([Employee_Main.DOB]) " _
& "WHERE (([Employee_Main].[Emp_ID]) = Chooseemp) ;"
DoCmd.RunSQL (strSQL_Birth_Year)
If (Emp_ID = ChooseEmp) Then
If (Month(Now()) <= Month(DOB)) Then
strSQL_Age = "UPDATE Employee_Main " _
& "SET Employee_Main.Age = Year(Now()) - Year([Employee_Main.DOB]) - 1 " _
& "WHERE (([Employee_Main].[Emp_ID]) = Chooseemp) ;"
DoCmd.RunSQL (strSQL_Age)
Else
strSQL_Age = "UPDATE Employee_Main " _
& "SET Employee_Main.Age = Year(Now()) - Year([Employee_Main.DOB]) " _
& "WHERE (([Employee_Main].[Emp_ID]) = Chooseemp) ;"
DoCmd.RunSQL (strSQL_Age)
End If
End If
If (Emp_ID = ChooseEmp) Then
If (Month(Now()) < Month(DOH)) Then
strSQL_YOS = "UPDATE Employee_Main " _
& "SET Employee_Main.YOS = Year(Now()) - Year([Employee_Main].[DOH]) - 1 " _
& "WHERE (([Employee_Main].[Emp_ID]) = Chooseemp) ;"
DoCmd.RunSQL (strSQL_YOS)
Else
strSQL_YOS = "UPDATE Employee_Main " _
& "SET Employee_Main.YOS = Year(Now()) - Year([Employee_Main].[DOH])" _
& "WHERE (([Employee_Main].[Emp_ID]) = Chooseemp) ;"
DoCmd.RunSQL (strSQL_YOS)
End If
End If
strSQL_Med_RT = "UPDATE Employee_Main, Deduction_Rates " _
& "SET Employee_Main.Med_Deduction_Amt = [Deduction_Rates].[Rate]" _
& "WHERE ( ((Employee_Main.[Medical
Enrollment])=[Deduction_Rates].[Enrollment_Selection]) " _
& "AND ((Deduction_Rates.Type)= 'Medical') " _
& "AND ((Employee_Main.[YOS]) >= [Deduction_Rates].[Start_Factor]) " _
& "AND ((Employee_Main.[YOS]) <= [Deduction_Rates].[End_Factor])" _
& "AND (([Employee_Main].[Emp_ID]) = Chooseemp) );"
DoCmd.RunSQL (strSQL_Med_RT)
strSQL_Dental_RT = "UPDATE Employee_Main, Deduction_Rates " _
& "SET Employee_Main.Dental_Deduction_Amt = [Deduction_Rates].[Rate]" _
& "WHERE ( ((Employee_Main.[Dental
Enrollment])=[Deduction_Rates].[Enrollment_Selection]) " _
& "AND ((Deduction_Rates.Type)= 'Dental') " _
& "AND ((Employee_Main.[YOS]) >= [Deduction_Rates].[Start_Factor]) " _
& "AND ((Employee_Main.[YOS]) <= [Deduction_Rates].[End_Factor])" _
& "AND (([Employee_Main].[Emp_ID]) = Chooseemp) );"
DoCmd.RunSQL (strSQL_Dental_RT)
strSQL_Vol_RT = "UPDATE Employee_Main, Deduction_Rates " _
& "SET Employee_Main.Vol_Rt = [Deduction_Rates].[Rate]" _
& "WHERE ( (([Employee_Main].[Emp_ID]) = Chooseemp) " _
& "AND ((Deduction_Rates.Type)= 'Voluntary') " _
& "AND ((Employee_Main.[Age]) >= [Deduction_Rates].[Start_Factor]) " _
& "AND ((Employee_Main.[Age]) <= [Deduction_Rates].[End_Factor])" _
& "AND ((Employee_Main.[Voluntary])= 'Yes') );"
DoCmd.RunSQL (strSQL_Vol_RT)
strSQL_Child_RT = "UPDATE Employee_Main, Deduction_Rates " _
& "SET Employee_Main.Child_Rt = [Deduction_Rates].[Rate]" _
& "WHERE ( (([Employee_Main].[Emp_ID]) = Chooseemp) " _
& "AND ((Deduction_Rates.Type)= 'Child') );"
DoCmd.RunSQL (strSQL_Child_RT)
strSQL_Med_Dental_Deduction_Amt = "UPDATE Employee_Main " _
& "SET Employee_Main.Med_Dental_Deduction_Amt =
Employee_Main.Med_Deduction_Amt + Employee_Main.Dental_Deduction_Amt " _
& "WHERE (([Employee_Main].[Emp_ID]) = Chooseemp) ;"
DoCmd.RunSQL (strSQL_Med_Dental_Deduction_Amt)
strSQL_Med_Dental_Deduction_Amt = "UPDATE Employee_Main " _
& "SET Employee_Main.Med_Dental_Deduction_Amt =
Employee_Main.Med_Deduction_Amt + Employee_Main.Dental_Deduction_Amt " _
& "WHERE (([Employee_Main].[Emp_ID]) = Chooseemp) ;"
DoCmd.RunSQL (strSQL_Med_Dental_Deduction_Amt)
strSQL_EE_Cost = "UPDATE Employee_Main " _
& "SET Employee_Main.EE_Cost = Employee_Main.Vol_Rt *
Employee_Main.Vol_EE_Units " _
& "WHERE (([Employee_Main].[Emp_ID]) = Chooseemp) ;"
DoCmd.RunSQL (strSQL_EE_Cost)
strSQL_Spouse_Cost = "UPDATE Employee_Main " _
& "SET Employee_Main.Spouse_Cost = Employee_Main.Vol_Rt *
Employee_Main.Vol_Spouse_Units " _
& "WHERE (([Employee_Main].[Emp_ID]) = Chooseemp) ;"
DoCmd.RunSQL (strSQL_Spouse_Cost)
strSQL_Child_Cost = "UPDATE Employee_Main " _
& "SET Employee_Main.Child_Cost = Employee_Main.Child_Rt *
Employee_Main.Vol_Child_Units " _
& "WHERE (([Employee_Main].[Emp_ID]) = Chooseemp) ;"
DoCmd.RunSQL (strSQL_Child_Cost)
strSQL_Vol_Ded_Amt = "UPDATE Employee_Main " _
& "SET Employee_Main.Voluntary_Deduction_Amt = Employee_Main.EE_Cost +
Employee_Main.Spouse_Cost + Employee_Main.Child_Cost " _
& "WHERE (([Employee_Main].[Emp_ID]) = Chooseemp) ;"
DoCmd.RunSQL (strSQL_Vol_Ded_Amt)
strSQL_Vol_Ded_Bi_Amt = "UPDATE Employee_Main " _
& "SET Employee_Main.Voluntary_Deduction_BiWeek_Amt =
((Employee_Main.EE_Cost + Employee_Main.Spouse_Cost +
Employee_Main.Child_Cost) *12 / 26) " _
& "WHERE (([Employee_Main].[Emp_ID]) = Chooseemp) ;"
DoCmd.RunSQL (strSQL_Vol_Ded_Bi_Amt)
strSQL_Tot_Ded = "UPDATE Employee_Main " _
& "SET Employee_Main.Total_Deductions =
Employee_Main.Med_Dental_Deduction_Amt +
Employee_Main.Voluntary_Deduction_BiWeek_Amt " _
& "WHERE (([Employee_Main].[Emp_ID]) = Chooseemp) ;"
DoCmd.RunSQL (strSQL_Tot_Ded)
Me.Refresh
Calculate_Deductions_Click_Exit:
Exit Sub
Calculate_Deductions_Click_Err:
MsgBox Error$
Resume Calculate_Deductions_Click_Exit
End Sub
The deduction rates are based off of age bands for voluntary life and years
of service (YOS) for medical and dental deductions. The values are stored in
a table called "Deduction_Rates" which contains the type of deduction
(Medical, Dental, Voluntary) along with Start and End factors (less than 1
YOS, less than or equal to a certain age, etc.) and finally the deduction
amount for each type and factor.
Given all of this information, how do I alter this code to loop through all
of my records and perform the functions listed with the click of one button?
I've considered creating a module which creates a recordsetclone, calls the
Calculate_Deductions function the Employee_Main form, and using a loop, but
would a Do Until .EOF Loop work? The function enters into the record defined
by the drop-down lists. Wouldn't I need to remove or change that?
Note to anyone who makes it through reading my code and answering my question:
I appreciate the time you took!!!
Thank you!
-Rachel