I'm writing a fairly basic application to automate vacation acrual rates based on years of service and then calculate current year-to-date acrued vacation time based on the employees acrual rate.
I've got the calculation for the length of employment working just fine with this function using DateDiff:
Public Function YearsOfEmployment(varDateOfHire As Variant) As Integer
Dim varAge As Variant
If IsNull(varDateOfHire) Then YearsOfEmployment = 0: Exit Function
varAge = DateDiff("yyyy", varDateOfHire, Now)
If Date < DateSerial(Year(Now), Month(varDateOfHire), _
Day(varDateOfHire)) Then
varAge = varAge - 1
End If
YearsOfEmployment = CInt(varAge)
End Function
- Public Function YearsOfEmployment(varDateOfHire As Variant) As Integer
- Dim varAge As Variant
- If IsNull(varDateOfHire) Then YearsOfEmployment = 0: Exit Function
- varAge = DateDiff("yyyy", varDateOfHire, Now)
- If Date < DateSerial(Year(Now), Month(varDateOfHire), _
- Day(varDateOfHire)) Then
- varAge = varAge - 1
- End If
- YearsOfEmployment = CInt(varAge)
- End Function
This is in it's own module and is called in an Access 2000 Form by this procedure call:
=YearsOfEmployment([date_of_hire])
(date_of_hire is the value stored in the employee_info table)
This works just fine and returns the current length of employment in years. Now here's where I'm stuck. To determine the employees acrual rate I need to evaluate how many years they have in and assign them the acrual rate they are entitled to. I've tried a variety of ideas, but I think this comes very close, but I'm missing something somewhere. I'm sure it's because the YearsOfEmployment variable is in a different function, and that's where I'm stuck -- getting that calculated result passed into this function:
Public Function Acrual(ByVal x As Variant) As Variant
' Dimension the variable.
Dim x As Variant
' Start the Select Case structure.
Set x = YearsOfEmployment
Select Case x
' Test to see if x less than or equal to 5.
Case Is <= 5
' Display a message box.
x = 6.67
' Test to see if x less than or equal to 10 and greater than 5.
Case 6 To 10
x = 10
' Test to see if x lgreater than 10.
Case Is > 10
x = 13.33
Acrual = CInt(x)
End Select
End Function
- Public Function Acrual(ByVal x As Variant) As Variant
- ' Dimension the variable.
- Dim x As Variant
- ' Start the Select Case structure.
- Set x = YearsOfEmployment
- Select Case x
- ' Test to see if x less than or equal to 5.
- Case Is <= 5
- ' Display a message box.
- x = 6.67
-
- ' Test to see if x less than or equal to 10 and greater than 5.
- Case 6 To 10
- x = 10
-
-
- ' Test to see if x lgreater than 10.
- Case Is > 10
- x = 13.33
- Acrual = CInt(x)
-
- End Select
- End Function
Like I said, I know this really can't be all that complicated to do, but I'm just a beginner whose figuring this stuff out as I go. (I'm only halfway through the VBA book I'm using and still have to work my way through the 2 Volume Access Developer's Handbooks *lol)
Once this part is solved the next step is to multiply the Acrual rate determined in the above procedure by the number of months already past in any given year. I already have the function that determines the number of months working just fine but I'm using an expression and I suspect I'm going to need to change that to a function similar to what I did for YearsOf Employment. THis is the expression I'm using that works
=IIf(Int(Format("1/1","y"))>Int(Format(Date(),"y")),DateDiff("m","1/1",Date())-1,DateDiff("m","1/1",Date()))
I think I can change that to a function OK, but I think I'm going to come up stuck again trying to get the result for Acrual to be used to calculate the number of vacation hours acrued year-to-date.
Any help would be much appreciated. I've been trying to get this to work for over 8 hours between the last three days. I figured that was enough effort on my own and time to ask for help. I'm probably making it more complicated than it really is.