Vacation Time Calculation - function

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

Related

How to get beginning to end of last week independent of today's date (Mysql)

I am trying to indicate all rows with last week's data (Monday-Sunday) using the following, gives me the data from (Tue-Mon) 2 feb-8 feb - so it is dynamic depending on the day of this week and tomorrow will give me the data from Wed-Tue. Any idea how to fix this:
case when `Date` >= date_sub(now(),INTERVAL 1 WEEK) and
`Date`< (date_sub(now(),INTERVAL 1 WEEK)+7) then 1 else 0 end
In mysql you could try using week() checking for the same week of curdate()
case when week(`Date`) = weeek(curdate())
AND year(`Date`) = year(curdate()) then 1 else 0 end
or for the previous week
case when week(`Date`) = weeek(curdate())-1
AND year(`Date`) = year(curdate()) then 1 else 0 end

Vacation Calculation based on Anniversary Date

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

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

Whats wrong with my MYSQL QUERY?

Can someone please tell me what is wrong with my query.
I am trying to fetch data fro my table based on the column called weekend, if weekend is set to "0" show from Sunday 6pm until Friday 9pm, then if weekend is set to "1" Show from Friday 9pm until Sunday 6pm.
SELECT *
FROM closures
WHERE closures.weekend = 0
OR WEEKDAY(NOW()) < 4
AND closures.weekend = 1
OR WEEKDAY(NOW()) > 4
OR (WEEKDAY(NOW())=4 AND HOUR(NOW())>21)
OR (WEEKDAY(NOW())=6 AND HOUR(NOW())<18)
It helps to phrase the question properly. What you should have said is "between 9pm Friday and 6pm Sunday I want to show the rows where closures.weekend = 1, otherwise show those where closures.weekend = 0".
Hence what you need to do is generate a value of 1 or 0 depending on whether it's the weekend or not, and then SELECT those rows where weekend has that value, i.e:
SELECT *
FROM
closures
WHERE
weekend = IF(
(WEEKDAY(NOW()) = 4 AND HOUR(NOW()) >= 21)
OR (WEEKDAY(NOW()) = 5)
OR (WEEKDAY(NOW()) = 6 AND HOUR(NOW()) < 18)
, 1, 0)
Weekend cant be both, 0 and 1
SELECT *
FROM
closures
WHERE
closures.weekend = 0
OR
(
WEEKDAY(NOW()) < 4
AND closures.weekend = 1
)
OR WEEKDAY(NOW()) > 4
OR
(WEEKDAY(NOW())=4 AND HOUR(NOW())>21)
OR
(WEEKDAY(NOW())=6 AND HOUR(NOW())<18)
My first guess is that the
SELECT field1, field2, ...
is missing ?

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.