Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
传递给Access VBA函数的日期值不正确_Vba_Datetime_Ms Access - Fatal编程技术网

传递给Access VBA函数的日期值不正确

传递给Access VBA函数的日期值不正确,vba,datetime,ms-access,Vba,Datetime,Ms Access,我试图根据员工的合同类型和状态计算假期余额。函数很简单,公式也很好用,但我无法从查询中获取函数来检索终止日期。终止日期的值为00:00:00,而终止日期为TRUE。 我试过使用Dlookup函数来收集数据,但运气不好,我想我正处于著名的访问日期诅咒之下。 提前谢谢 我的查询如下所示: 我的职能是: Public Function LeaveBalanceCalc(EmployeeType As String, HiredDate As Date, Terminated As Boolean,

我试图根据员工的合同类型和状态计算假期余额。函数很简单,公式也很好用,但我无法从查询中获取函数来检索终止日期。终止日期的值为00:00:00,而终止日期为TRUE。 我试过使用Dlookup函数来收集数据,但运气不好,我想我正处于著名的访问日期诅咒之下。 提前谢谢

我的查询如下所示:

我的职能是:

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
大家好

对我的问题有更多的背景知识。该函数用于计算员工寿命所赚取的休假天数。我的目标是向查询中添加一个名为Balance的计算列。请参见上面的查询,该查询包含员工信息

我试过其他方法;例如,下面的函数代码计算正确,只有终止的员工和所有其他员工按0日期计算,而不是现在。下面的替代函数使用TerminateDate本身来确定和计算余额。如果TerminateDate设置为Now,但使用00:00:00日期重新运行。 以下是替代功能

Public Function LeaveBalanceCalc(EmployeeType As String, HiredDate As Date, TerminatedDate As Date) As Double

    '................................................................................................................................
    'Name:   LeaveBalanceCalc
    'Inputs: EmployeeType as string
    '        HiredDate as date
    '        TerminatedDate as date
    'Return: as Double
    'Date:   07/02/2020
    'Comment:This function is to calculate the earned leave days by longevity
    '.................................................................................................................................


    Dim EarningRate As Double
    Dim FirstMonthEarning As Double
    Dim LastMonthEarning As Double

    'First we need to determine the earning rate for the employee
        Select Case EmployeeType
            Case Is = "International"
                EarningRate = 4
            Case Else
                EarningRate = 1.25
        End Select


        If Nz(TerminatedDate) Then

            TerminatedDate = Now()

        End If

                FirstMonthEarning = (DateDiff("d", HiredDate, FirstOfMonth(HiredDate)) + 1) * (EarningRate / DateDiff("d", FirstOfMonth(HiredDate), LastOfMonth(HiredDate)))
                LastMonthEarning = (DateDiff("d", FirstOfMonth(TerminatedDate), TerminatedDate) + 1) * (EarningRate / (DateDiff("d", FirstOfMonth(TerminatedDate), LastOfMonth(TerminatedDate)) + 1))
                LeaveBalanceCalc = ((DateDiff("m", HiredDate, TerminatedDate)) * EarningRate) + FirstMonthEarning + LastMonthEarning

    End Function

如果我能让Either函数正常工作,我会很高兴。

欢迎使用S/O。虽然我不使用VB/Access编程,但我可以提供这么多

日期就是日期就是日期。。。为什么一直试图转换为DateSerial。“日期”数据类型不关心基于月/日与日/月的国际首选项的可视输出表示,它以日期格式存储。日期就是今天

也就是说,我将再次简化如下,不是一个VB程序员,在需要的地方调整语法。您已经将变量enddate声明为日期字段,为什么要要求它的日期序列。它已经是一个日期了,只需使用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
现在,在Excel中使用Dates和DateDiff进行测试之后,您不需要为计算去除粒度小时/分钟,在测试day或任何其他部分时,这些计算将被忽略

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)
然后,你有两个计算日期。。。endDate via 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)

我不知道著名的访问日期诅咒是什么,但是在is=True块的情况下,您的enddate被注释掉了,但是您正在引用它是的,我确实从代码中注释掉了它,但是我在公式中使用了TerminateDate。当我第一次写TRUE时,case和False的公式是一样的。这只是一次毫无希望的尝试。祸根在于access VBA和query中美国日期和英国日期格式之间的混淆。LeaveBalanceCalc使用enddate。你甚至需要使用DateSerial吗?看起来您可以使用已有的日期变量。我已经按照您的建议修改了代码。现在,它使用enddate来表示这两种情况。你说得对,我可以在enddate的公式中更改dateserial。公式是从早期版本中复制的,所以dateserial仍然存在。这并不能回答问题,但请同时修改问题的标题。它对未来的读者没有任何用处。谢谢你的帮助。现在我有了一个干净的代码,但仍然终止日期传递函数为00:00:00@OpsMan,然后我会先走一步。。。你是如何从哪里得到这些的。。。它是来自一个查询还是其他?如果是这样,请编辑您的原始帖子,转到底部并发布附加信息。不要试图在评论中提供更多这样的细节。。。