Vacation Calculation based on Anniversary Date - function

The rule is after you first year of employment, based on your anniversary date, you get 40 hours. (ex. I was hired on 05/12/2011, I get 40 hours vacation on 05/12/2012.)
Then each subsequent year it rolls to calendar year. So using my above example I need to use my 40 hours between 05/12/2012 and 12/31/2012, because on 01/01/2013 my forty starts over.
At three years (01/01/2014), calendar date, I would get 80 hrs.
At 10 years, calendar date, it goes to 120. The code I am using is below but it doesn't come out right...
Function calcVacEarned(asOfDate As Date, HireDate As Date)
Dim anniversary As Boolean
Dim yos As Integer
anniversary = False
yos = Year(asOfDate) - Year(HireDate)
If Month(HireDate) = Month(asOfDate) And (yos = 1) Then
If Day(HireDate) <= Day(asOfDate) Then
anniversary = True
End If
ElseIf Month(HireDate) < Month(asOfDate) And (yos = 1) Then
anniversary = True
End If
If anniversary Then
calcVacEarned = 40
Else
Select Case yos
Case Is > 10
calcVacEarned = 120
Case Is > 3
calcVacEarned = 80
Case Is > 1
calcVacEarned = 40
Case Else
calcVacEarned = 0
End Select
End If
End Function
My company uses this on a daily basis to calculate hours across the board, could use some help. Thanks in advance!

Assuming you're returning the correct integer for yos :
If anniversary Then
Select Case yos
Case 1
MsgBox "You actually get vacation"
Case 2
MsgBox "OP said nothing regarding 2 years!"
Case 3
MsgBox "You get 80 hours!"
Case 10
MsgBox "You get 120 hours!"
End Select
End If

Related

VB.NET Do Until Loop to get the Month Name

I want to get the month name from a Do Until......Loop and display in a CheckedListBox.
I have two tables.
1. Month_Count
2. Fees_Ledger
My month name function...
Function mnthName(ByVal mnth As Integer)
Dim name As String = String.Empty
name = MonthName(mnth, False)
Return name
End Function
To get the Month Details
"SELECT FromMonth,ToMonth,MonthCount FROM Month_Count WHERE SemesterNumber='1'"
And to get the Paid Count
"SELECT COUNT(*) AS TotMonthPaidCount FROM Fees_Ledger WHERE SemYear='1' AND FeeId='1'"
So...
' Got the values from queries
FromMonth = 7 '(July)
ToMonth = 6 '(June)
MonthCount = 12 '(Loop will rotate 12 times)
TotMonthPaidCount = 1 '(FeeId 1 paid one time)
'Declaring an integer variable
Dim StartM As Integer = 1
FromMonth = FromMonth + TotMonthPaidCount
' For the first month
CheckedListBoxMonth.Items.Clear()
CheckedListBoxMonth.Items.Add(mnthName(FromMonth))
' Now the loop to achieve the goal
Do Until StartM = (MonthCount - TotMonthPaidCount)
If FromMonth >= 12 Then
FromMonth = 1
CheckedListBoxMonth.Items.Add(mnthName(FromMonth))
Else
FromMonth += 1
CheckedListBoxMonth.Items.Add(mnthName(FromMonth))
End If
StartM += 1
Loop
This Subroutine results exactly what I want.
But the problem occurs when the StartMonth = FromMonth (6) + TotMonthPaidCount (7) value >12. As 13 or 14 or 15 has no Month Name, it is showing error.
Argument 'Month' is not a valid value.
I want it like below.
What should I do ?

Sum values in one field based on date range in another

I have two tables, both with dates (which is what they are/will be joined on)
Table 1 has daily rainfall values
Table 2 has weekly water volume values.
I'm trying to get Access to calculate the weekly rainfall (from the daily values) based on the dates that are given for the water volumes ie: total rainfall between two dates calculated dynamically. I have some experience with using Access SQL but I am stumped on this one. To add to the complication, every once in a while the volume values aren't always 7 days apart.
One method is to find the week numbers from each table, then join between these:
Select
Sum(Table1.Volume) As Volume1,
ISO_Weeknumber(Table1.[datefield]) As WeekNumber1,
Sum(Table2.Volume) As Volume2,
ISO_Weeknumber(Table2.[datefield]) As WeekNumber2
From
Table1
Inner Join
Table2
On ISO_Weeknumber(Table1.[datefield])=ISO_Weeknumber(Table2.[datefield])
Group By
ISO_Weeknumber(Table1.[datefield]),
ISO_Weeknumber(Table2.[datefield])
using this function:
Public Function ISO_WeekYearNumber( _
ByVal datDate As Date, _
Optional ByRef intYear As Integer, _
Optional ByRef bytWeek As Byte) _
As String
' Calculates and returns year and week number for date datDate according to the ISO 8601:1988 standard.
' Optionally returns numeric year and week.
' 1998-2007, Gustav Brock, Cactus Data ApS, CPH.
' May be freely used and distributed.
Const cbytFirstWeekOfAnyYear As Byte = 1
Const cbytLastWeekOfLeapYear As Byte = 53
Const cbytMonthJanuary As Byte = 1
Const cbytMonthDecember As Byte = 12
Const cstrSeparatorYearWeek As String = "W"
Dim bytMonth As Byte
Dim bytISOThursday As Byte
Dim datLastDayOfYear As Date
intYear = Year(datDate)
bytMonth = Month(datDate)
bytWeek = DatePart("ww", datDate, vbMonday, vbFirstFourDays)
If bytWeek = cbytLastWeekOfLeapYear Then
bytISOThursday = Weekday(vbThursday, vbMonday)
datLastDayOfYear = DateSerial(intYear, cbytMonthDecember, 31)
If Weekday(datLastDayOfYear, vbMonday) >= bytISOThursday Then
' OK, week count of 53 is caused by leap year.
Else
' Correct for Access97/2000+ bug.
bytWeek = cbytFirstWeekOfAnyYear
End If
End If
' Adjust year where week number belongs to next or previous year.
If bytMonth = cbytMonthJanuary Then
If bytWeek >= cbytLastWeekOfLeapYear - 1 Then
' This is an early date of January belonging to the last week of the previous year.
intYear = intYear - 1
End If
ElseIf bytMonth = cbytMonthDecember Then
If bytWeek = cbytFirstWeekOfAnyYear Then
' This is a late date of December belonging to the first week of the next year.
intYear = intYear + 1
End If
End If
ISO_WeekYearNumber = CStr(intYear) & cstrSeparatorYearWeek & Format(bytWeek, "00")
End Function
For this to work, you must have all weeks in Table1 that exist in Table2 and vice versa.

Calculate Vacation on Anniversary date then Calendar year

I have a form in my Access database that calculated Vacation based on my company policy. The rule is after you first year of employment based on your anniversary date you get 40 hours. (ex. I was hired on 09/06/2012, I get 40 hours vacation on 09/06/2013.)
Then each subsequent year it rolls to calendar year. So using my above example I need to use my 40 hours between 09/06/2013 and 12/31/2013 or I lose them because on January 1, 2014 I recieve 40.
At three years, calendar date, I would get 80 hrs. for on 01/01/2016 I get 80 hours.
At 10 years, calendar date, it goes to 120. The code I have been using which continues to pull from the anniversary date follows:
Function calcVacEarned(asOfDate As Date, HireDate As Date)
Dim yos As Single
Dim curryear As Integer
Dim hireYear As Integer
curryear = Year(asOfDate)
hireYear = Year(HireDate)
yos = (asOfDate - HireDate) / 365.25
If yos >= 1 Then
Select Case yos
Case Is > 10
calcVacEarned = 120
Case Is > 3
calcVacEarned = 80
Case Is > 1
calcVacEarned = 40
Case Else
calcVacEarned = 0
End Select
Else
If hireYear >= curryear Then
calcVacEarned = "0"
Else
If DateSerial(curryear, Month(HireDate), Day(HireDate)) = asOfDate Then
calcVacEarned = "40"
Else
calcVacEarned = "0"
End If
End If
End If
End Function
My company uses this on a daily basis to calculate hours across the board, could use some help. Thanks in advance!
I hope I have understood what you are asking. Here goes :
Function calcVacEarned(asOfDate As Date, HireDate As Date)
Dim anniversary As Boolean
Dim yos as Integer
anniversary = False
yos = Year(asOfDate) - Year(HireDate)
if Month(HireDate) = Month(asOfDate) And (yos = 1) then
if Day(HireDate) <= Day(asOfDate) then
anniversary = True
end if
elseif Month(HireDate) < Month(asOfDate) And (yos = 1) then
anniversary = True
end if
if anniversary then
calcVacEarned = 40
else
Select Case yos
Case Is > 10
calcVacEarned = 120
Case Is > 3
calcVacEarned = 80
Case Is > 1
calcVacEarned = 40
Case Else
calcVacEarned = 0
End Select
end if
End Function

Vacation Time Calculation

On my Access Database form I have a field that contains a calculation for Vacation time. The current formula is as follows:
Function calcVacEarned(asOfDate As Date, HireDate As Date)
Dim yos As Single
yos = (asOfDate - HireDate) / 365.25
Select Case yos
Case Is > 10
calcVacEarned = 120
Case Is > 3
calcVacEarned = 80
Case Is > 1
calcVacEarned = 40
Case Else
calcVacEarned = 0
End Select
End Function
However, the time is not coming out correctly. Our vacation time runs from Anniversary date to Anniversary date for the first year and Calendar year the following years.
For example, if I was hired on 11/02/2010, my Anniversary date is 11/02/2011 which will give me 1 year of service and 5 days of vacation. (So I have from 11/02/2011 - 12/31/2011 to take my 5 days or lose them)
Then on 01/01/2012 my vacation time will start over at 5 days going into year 2. (I have from 01/01/2012 - 12/31/2012 to take these 5 days.
On 01/01/2013 I will roll into my 3rd year and recieve 10 days.
I need the function above to represent this... Help!! Thanks
I think if I understand your problem correctly this will do what you want. You just need to change the hire date for years that are greater than 3.
Function calcVacEarned(asOfDate As Date, HireDate As Date)
calcVacEarned = 0
Dim yos As Single
yos = (asOfDate - HireDate) / 365.25
If yos > 1 And yos < 2 Then
calcVacEarned = 40
ElseIf yos > 2 Then
HireDate = "01/01/" & Year(HireDate)
End If
yos = (asOfDate - HireDate) / 365.25
Select Case yos
Case Is > 10
calcVacEarned = 120
Case Is > 3
calcVacEarned = 80
End Select
End Function

Find number of weekdays/weekends in a given date range

I'm trying to find some VBA code to determine the number of week days and weekend days in a given date range using Access VBA.
For example:
Begin Date - 1/1/2012
End Date - 1/31/2012
Result should be:
Week days - 22
Weekend days - 9
Can anyone help out with this?
These two functions will calculate the number of weekdays and weekend days:
Function NumWeekendDays(dBegin As Date, dEnd As Date) As Long
Dim iPartial As Integer
Dim lBeginDay As Long
Dim lNumWeekendDays As Long
iPartial = DateDiff("d", dBegin, dEnd + 1) Mod 7
lBeginDay = 6 - DatePart("w", dBegin, vbMonday)
lNumWeekendDays = (DateDiff("d", dBegin, dEnd + 1) \ 7) * 2
If iPartial > 0 And lBeginDay - iPartial < 0 Then
If lBeginDay = -1 Then
lNumWeekendDays = lNumWeekendDays + 1
ElseIf iPartial - lBeginDay = 1 Then
lNumWeekendDays = lNumWeekendDays + 1
Else
lNumWeekendDays = lNumWeekendDays + 2
End If
End If
NumWeekendDays = lNumWeekendDays
End Function
Function NumWeekDays(dBegin As Date, dEnd As Date) As Long
NumWeekDays = DateDiff("d", dBegin, dEnd + 1) - NumWeekendDays(dBegin, dEnd)
End Function
Note: I found it simplest to calculate the partial-week weekend days by calculating the lBeginDay variable so that if the start date was Monday, lBeginDay == 5... if the start date was Friday, lBeginDay == 1, etc. Other variations should also work.