计算积累的休假日

时间:2017-01-18 23:23:53

标签: database ms-access

我有一个2007使用的MS Access数据库,我想添加一个子表单,显示基于员工的休假计算等。我有一个我在网上找到的模块,用于计算两个日期之间的经过时间,并给出1年2个月3天或2年等结果。我需要让结果给我假期,而不是几年,几个月和几天。在我的查询中,它将基于[DateOfHire]和Date()

  • 1年服务= 1周假期
  • 2年服务= 2周假期
  • 8年服务= 3周假期
  • 15年服务= 4周假期
  • 25年服务= 5周假期

-

Public Function fElapsedTimeYMD(varStartDate As Variant, varEndDate) As String

    Dim dtToday As Date
    Dim intStartYear As Integer, intStartMonth As Integer, intStartDay As Integer
    Dim intEndYear As Integer, intEndMonth As Integer, intEndDay As Integer
    Dim intTmpYear As Integer, intTmpMonth As Integer, intTmpDay As Integer
    Dim strYear As String, strMonth As String, strDay As String
    Dim sngLeap As Single

    If Not IsDate(varStartDate) Or Not IsDate(varEndDate) Then Exit Function

    dtToday = Date
    intStartYear = Year(varStartDate)
    intStartMonth = Month(varStartDate)
    intStartDay = Day(varStartDate)
    intEndYear = Year(varEndDate)
    intEndMonth = Month(varEndDate)
    intEndDay = Day(varEndDate)

    If intStartDay > intEndDay Then
        Select Case intEndMonth
        Case Is = 1
            intEndDay = intEndDay + 31
            intEndMonth = 12
            intEndYear = intEndYear - 1
        Case Is = 2    ' Check for Leap Year
            If varStartDate = DateSerial(intEndYear, 2, 29) Then
                intEndDay = intEndDay + 29
                intEndMonth = intEndMonth - 1
            Else
                intEndDay = intEndDay + 28
                intEndMonth = intEndMonth - 1
            End If
        Case Is = 4, 6, 9, 11
            intEndDay = intEndDay + 30
            intEndMonth = intEndMonth - 1
        Case Is = 3, 5, 7, 8, 10, 12
            intEndDay = intEndDay + 31
            intEndMonth = intEndMonth - 1
        End Select
    End If

    If intStartMonth > intEndMonth Then
        Select Case intEndMonth
        Case Is = 1
            intEndMonth = 13
            intEndYear = intEndYear - 1
        Case Else
            intEndMonth = intEndMonth + 12
            intEndYear = intEndYear - 1
        End Select
    End If

    intTmpYear = intEndYear - intStartYear
    intTmpMonth = intEndMonth - intStartMonth
    intTmpDay = intEndDay - intStartDay

    Select Case intTmpYear
    Case Is = 0
        strYear = ""
    Case Is = 1
        strYear = Trim(Str(intTmpYear)) & " Year"
    Case Else
        strYear = Trim(Str(intTmpYear)) & " Years"
    End Select

    Select Case intTmpMonth
    Case Is = 0
        strMonth = ""
    Case Is = 1
        strMonth = Trim(Str(intTmpMonth)) & " Month"
    Case Else
        strMonth = Trim(Str(intTmpMonth)) & " Months"
    End Select

    Select Case intTmpDay
    Case Is = 0
        strDay = ""
    Case Is = 1
        strDay = Trim(Str(intTmpDay)) & " Day"
    Case Else
        strDay = Trim(Str(intTmpDay)) & " Days"
    End Select

    fElapsedTimeYMD = Trim(strYear & " " & strMonth & " " & strDay)

End Function

2 个答案:

答案 0 :(得分:0)

你可以使用约会对象

https://www.techonthenet.com/access/functions/date/datediff.php

做一个选择案例,根据持续时间边界,然后返回一个可用周假期的整数

如果你添加了这个,你应该得到这些年份。

Dim yearsService As Integer
yearsService = DateDiff ("yyyy", varStartDate , varEndDate)

这应该产生整数结果

答案 1 :(得分:0)

TotalVacWeeks: IIf([YearsOfService]<=0,0,IIf([YearsOfService]<=1,1,IIf([YearsOfService]<=7,2,IIf([YearsOfService]<=14,3,IIf([YearsOfService]<=24,4,IIf([YearsOfService]>=25,5))))))+IIf([BoughtVac]=True,1,0)