【问题标题】:Access VBA check that period between two date fields does not cross specific date访问 VBA 检查两个日期字段之间的时间段是否不跨越特定日期
【发布时间】:2016-05-27 05:06:18
【问题描述】:

我有一个 Access 表单,要求用户输入 [开始日期] 和 [结束日期]。在保存记录之前,我想验证这些字段以确保输入的期间在任何给定年份都不会超过 6 月 30 日。

在我的脑海中,我认为程序会首先检查 [开始日期],如果它在 1 月 1 日和 6 月 30 日之间,那么 [结束日期] 必须小于或等于 6 月 30 日。如果 [开始日期] 介于 7 月 1 日和 12 月 31 日之间,则 [结束日期] 必须小于或等于下一年的 6 月 30 日。

我不确定如何在 VBA 中表达这一点,因此我们将不胜感激。

【问题讨论】:

    标签: ms-access vba


    【解决方案1】:

    我认为可能是这样的:

    Public Function IsDateValid(dtStart As Date, dtEnd As Date) As Boolean
    Dim dtDeadline As Date
    
    dtDeadline = DateSerial(Year(dtStart), 6, 30)
    If dtStart > dtDeadline Then
        dtDeadline = DateSerial(Year(dtStart) + 1, 6, 30)
    End If
    IsDateValid = dtEnd < dtDeadline
    End Function
    

    【讨论】:

      【解决方案2】:

      你可以按照这个方法:

      Dim StartDate As Date
      Dim EndDate   As Date
      Dim CheckDate As Date 
      Dim OK        As Boolean
      
      ' Pass values of StartDate and EndDate here.
      ' StartDate = ?
      ' EndDate = ?
      
      ' Find June 30th of the current or the next year of StartDate.
      CheckDate = DateSerial(Year(StartDate) + (Month(StartDate) - 1) \ 6, 6, 30)  
      
      If DateDiff("d", EndDate, CheckDate) >= 0 Then
          ' EndDate falls before or on CheckDate.
          OK = True
      End If
      
      ' Return value of OK.
      

      请注意反斜杠执行整数除法。

      【讨论】:

      • 感谢您的反馈,我能够通过以下解决方案的变化来实现我的目标
      猜你喜欢
      • 2016-05-31
      • 2016-07-10
      • 2012-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-29
      相关资源
      最近更新 更多