如何找到明天和昨天的日期

时间:2017-09-20 03:33:30

标签: ms-access access-vba

我想过滤一个表单,以显示明天和昨天的日期。

使用以下宏

,表单今天运行良好
if [Forms]![issues]![OpenedDateFilter]="today"
applyfilter
where condition
(Year([Opened Date])=Year(Date()) And Month([Due Date])=Month(Date()) And 
Day([Opened Date])=Day(Date()))

感谢您提供任何帮助或指示。

1 个答案:

答案 0 :(得分:0)

你必须考虑到“明天”和“昨天”可能在另一年落下。因此,请使用 DateAdd

Dim Offset As Integer

Select Case [Forms]![issues]![OpenedDateFilter]
    Case "tomorrow"
        Offset = 1
    Case "yesterday"
        Offset = -1
    Case Else
        Offset = 0
End Select

Me.Filter = "[Opened Date] = #" & Format(DateAdd("d", Offset, Date), "yyyy\/mm\/dd") & "#"
Me.FilterOn = True

如果[Due Date]不是拼写错误,您可以使用 DateSerial

Me.Filter = "[Opened Date] = #" & Format(DateSerial(Year(Date), Month([Due Date]), Day(Date) + Offset), "yyyy\/mm\/dd") & "#"