【问题标题】:Access - Use date if available, else use today's date访问 - 使用日期(如果有),否则使用今天的日期
【发布时间】:2017-06-28 17:20:04
【问题描述】:

所以这让我发疯了,它应该非常容易做到。我不知道为什么我不能让它工作。 :( 目前我的函数正在计算开始日期和今天日期之间的差异,它工作得很好,但现在我需要实现一个可选的结束日期。所以如果有可用的 EmpEndDate,则应该使用它,否则为今天的日期应该使用。

这是我的功能之一:

Public Function RetentionMonths(ByVal EmpStartDate As Date) As Integer

    Dim RetentionStartdate As Date
    Dim FollowingMonth As Date

        RetentionStartdate = DateAdd("ww", 14, DateAdd("d", vbSaturday - Weekday(EmpStartDate), EmpStartDate))
        FollowingMonth = DateSerial(Year(RetentionStartdate), Month(RetentionStartdate) + 1, 1)
        RetentionMonths = Months(FollowingMonth, Date)

End Function

编辑:Months() 函数

Public Function Months( _
  ByVal datDate1 As Date, _
  ByVal datDate2 As Date, _
  Optional ByVal booLinear As Boolean) _
  As Integer

  Dim intDiff   As Integer
  Dim intSign   As Integer
  Dim intMonths As Integer

  intMonths = DateDiff("m", datDate1, datDate2)

  If DateDiff("d", datDate1, datDate2) > 0 Then
    intSign = Sgn(DateDiff("d", DateAdd("m", intMonths, datDate1), datDate2))
    intDiff = Abs(intSign < 0)
  Else
    intSign = Sgn(DateDiff("d", DateAdd("m", -intMonths, datDate2), datDate1))
    If intSign <> 0 Then
      intDiff = Abs(booLinear)
    End If
    intDiff = intDiff - Abs(intSign < 0)
  End If

  Months = intMonths - intDiff

End Function

我尝试解决这个问题的一种方法:

Public Function RetentionMonths(ByVal EmpStartDate As Date, _
    Optional ByVal EmpEndDate As Date) _
    As Integer

    Dim RetentionStartdate As Date
    Dim FollowingMonth As Date

        RetentionStartdate = DateAdd("ww", 14, DateAdd("d", vbSaturday - Weekday(EmpStartDate), EmpStartDate))
        FollowingMonth = DateSerial(Year(RetentionStartdate), Month(RetentionStartdate) + 1, 1)
        RetentionMonths = Months(FollowingMonth, Nz(EmpEndDate, Date))

End Function

我要么在过去有用的字段中得到疯狂的负数,要么#Name?错误。

【问题讨论】:

  • “休息”是什么意思?你得到什么错误? EmpEndDate 未在任何表达式中引用。也许您需要使用 Nz() 函数。你有一个自定义函数 Months()?内在函数是 Month()。
  • 我没有看到你在你提供的代码中的任何地方声明 EmpEndDate 那么这个变量是从哪里来的?
  • @Sorceri 它也没有在函数的任何地方引用。也许OP的意思是把它作为参数添加?
  • 对于没有提供足够的信息,我深表歉意。我已经更新了我的问题。

标签: date ms-access vba


【解决方案1】:

您可以使用 clng 检查结束日期的值 0

Public Function RetentionMonths(ByVal EmpStartDate As Date, _
    Optional ByVal EmpEndDate As Date) _
    As Integer

    Dim RetentionStartdate As Date
    Dim FollowingMonth As Date
    Dim tempEndDate As Date

    If CLng(EmpEndDate) = 0 Then EmpEndDate = Now()


        RetentionStartdate = DateAdd("ww", 14, DateAdd("d", vbSaturday - Weekday(EmpStartDate), EmpStartDate))
        FollowingMonth = DateSerial(Year(RetentionStartdate), Month(RetentionStartdate) + 1, 1)
        RetentionMonths = Months(FollowingMonth, Nz(EmpEndDate, Date))

End Function

【讨论】:

  • 有效!这是一个奇迹!但仅当输入了 EmpStartDate 时。否则,我会得到#Type!错误。这是为什么呢?
  • @Zuzu 你有一个我不知道的 Nz 函数。我运行这个给出了今天之前 45 天的开始日期和现在的结束日期。删除它返回 -2 的 Nz 函数,所以你的错误可能在那里?...
  • 您不必转换为数字。日期就是日期,所以:If EmpEndDate = #00:00# Then EmpEndDate = Date.
  • 人力资源部。我显然不明白。我在 VBA 中尝试了以下(以及一些细微的变化): ?RetentionMonths(2015-01-01, Nz(EmpEndDate, Date)) 和 ?RetentionMonths(2015-01-01, Nz(2017-06-29,日期))......我得到1339或-4。确认。对不起,我是个菜鸟。 :( 我也显然不知道如何在这里格式化 cmets ......对不起文本块。
  • @Zuzu 将 NZ 函数添加到您的问题中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-12-25
  • 1970-01-01
  • 1970-01-01
  • 2018-05-24
  • 1970-01-01
  • 2012-12-06
  • 1970-01-01
相关资源
最近更新 更多