【问题标题】:Upcoming Date Function即将到来的日期功能
【发布时间】:2021-08-18 23:01:11
【问题描述】:

我有 3 个不同的日期,我正在尝试编写最接近的即将到来的日期。如果日期是过去的,则需要忽略它。另外,date1

我已尝试使用 (8/18/2021, 8/19/2021, 8/20/2021) 作为输入的以下代码,但函数返回 12:00:00AM 而不是 8/18/2021

Public Function UpcomingDates(date1 As Date, date2 As Date, date3 As Date) As Date

Dim ClosestDate As Date

If date1 >= Date Then   'diff1 >= 0
    ClosestDate = date1
ElseIf date2 >= Date Then
    ClosestDate = date2
ElseIf date2 >= Date Then
    ClosestDate = date3
End If

End Function

为什么函数不返回 8/18/2021?

*我在 MS Access Query 中使用 VBA

【问题讨论】:

  • 您还没有设置函数返回值,只是声明了它将返回的数据类型 - 尝试在 End Function 之前添加 UpcomingDates=ClosestDate

标签: ms-access


【解决方案1】:

日期作为序列号存储在 Access(和其他办公产品)中。例如,“1/1/2008”实际上存储为 39448。数字 39448 是自 1900 年 1 月 1 日以来的天数。这个数字的小数表示一天中的时间。因此,假设没有小数的数据值是 12:00:00 AM。例如,39448.25 将是 2008 年 1 月 1 日上午 6 点,39448.5 将是 2008 年 1 月 1 日下午 12 点,等等。

要让函数只显示日期,请使用格式函数:

Format(ClosestDate, "m/d/yyyy")

【讨论】:

  • 您好,还有一个关于此的问题。如果所有三个日期都已过去,我如何让函数不返回任何内容,以使数据表视图中的 Access 字段为空白?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多