我正在尝试根据员工的合同类型和状态来计算请假余额。该函数很简单,公式可以正常工作,但是我无法从查询中检索到终止日期的函数。终止日期为00:00:00,而终止为TRUE。我已经尝试过Dlookup函数来收集数据,但是没有运气,我想我处于Access的著名日期诅咒之下。提前致谢。
我的查询如下:qryLeaveBalance
和我的功能:
Public Function LeaveBalanceCalc(EmployeeType As String, HiredDate As Date, Terminated As Boolean, Optional TerminatedDate As Date) As Double
Dim EarningRate As Double
Dim FirstMonthEarning As Double
Dim LastMonthEarning As Double
Dim enddate As Date
'First to determine the earning rate for the employee
Select Case EmployeeType
Case Is = "International"
EarningRate = 4
Case Else
EarningRate = 1.25
End Select
enddate = Now()
If Terminated = True Then
enddate = TerminatedDate
End If
FirstMonthEarning = (DateDiff("d", HiredDate, LastOfMonth(HiredDate)) + 1) * (EarningRate / (DateDiff("d", FirstOfMonth(HiredDate), LastOfMonth(HiredDate)) + 1))
LastMonthEarning = (DateDiff("d", FirstOfMonth(enddate), enddate) + 1) * (EarningRate / (DateDiff("d", FirstOfMonth(enddate), LastOfMonth(enddate)) + 1))
LeaveBalanceCalc = FormatNumber((DateDiff("m", HiredDate, enddate) - 2) * EarningRate + FirstMonthEarning + LastMonthEarning, 2)
End Function
欢迎使用S / O。尽管我不使用VB / Access编程,但我可以提供这么多...
A Date is a Date is a Date ...为什么要继续尝试转换为DateSerial()。 “日期”数据类型并不关心基于月/日与日/月的国际首选项的视觉输出表示,而是以日期格式存储。日期就是现在。
就是说,我将简化如下(同样,不是VB程序员,请根据需要调整语法)。您已经将变量“ enddate”声明为日期字段,为什么要要求它的DateSerial()。它已经是一个日期,只需使用Now()值。之后,如果提供终止日期,则将其拖入结束日期以取代它。现在您知道为计算目的您将获得正确的日期。
' no matter what, default the end date to whatever now is
enddate = Now()
' if terminated, use this to supersede
If Terminated = True
enddate = TerminatedDate
End If
现在,在使用Dates和DateDiff()在Excel中进行测试之后,您无需删除细化的小时/分钟来进行计算,而在测试“ d” ay(或其他任何部分)时,这些计算将被忽略。] >
a DateDiff() by day of ex : "2020-02-10 12:00:00AM" and "2020-02-11 12:00:00AM" will result in 1 DAY AND: "2020-02-10 12:00:00AM" and "2020-02-11 11:47:32PM" will result in 1 DAY AND: "2020-02-11 11:47:32PM" and "2020-02-12 12:00:00AM" and will result in 1 DAY (even though it is only just over 12 minutes away, it still considers it as 1 day)
然后,您有2个用于计算的日期... endDate(通过Now()或终止)和雇用日期。完成计算
FirstMonthEarning = (DateDiff("d", HiredDate, LastOfMonth(HiredDate)) + 1) * (EarningRate / (DateDiff("d", FirstOfMonth(HiredDate), LastOfMonth(HiredDate)) + 1))
LastMonthEarning = (DateDiff("d", FirstOfMonth(enddate), enddate) + 1) * (EarningRate / (DateDiff("d", FirstOfMonth(enddate), LastOfMonth(enddate)) + 1))
LeaveBalanceCalc = FormatNumber(((DateDiff("m", HiredDate, enddate ) - 2) * EarningRate) + FirstMonthEarning + LastMonthEarning, 2)