MS Access 2010(设计视图):return 本周的星期一,星期一作为一周的第一天
MS Access 2010 (Design View): return Monday of the current week with Monday as 1st day of the week
我需要使我的 Access 查询始终 return 在本周的星期一。我在 Google/Whosebug 上看到了一些解决方案,但它们是用 SQL 编写的,我是创建 Access 查询的初学者(我正在使用设计视图来创建它们)。
目标:周应被视为 M T W T F S S。 然后,查询应始终 return 当前周的星期一。因此,如果是星期天,应该还是 return 前一个星期一, 而不是 下周的星期一。谁能解释如何使用 Access 2010 中的设计视图执行此操作?
请记住,在这种情况下,我们使用的是日期,因此如果我们这样做 Date() - 1
,我们将得到今天之前的 1 天。
Date()
~ 今天日期
DatePart(
"w" - Weekday
Date() - Today's date
2 - vBMonday (Access assumes Sunday is the first day of the week, which is why this is necessary.)
1 - vbFirstJan1 - This gets into using the first week of the year. We could have omitted this, as 1 is the default.
)
-1 - Subtract 1 from the DatePart value.
值
Date() = 4/27/2015 (at time of this writing)
DatePart("w",Date(),2,1) = 1
DatePart("w",Date(),2,1)-1 = 0
所以我们有 Date()-0
...好吧,这有什么了不起的?好吧,让我们看一个更有用的场景,今天的日期不是星期一。
假设今天是 4/28/2015(星期二)
Date() = 4/28/2015
DatePart("w",Date(),2,1) = 2
DatePart("w",Date(),2,1)-1 = 1
所以,从外到内;给我当前的工作日值。 (1 = 星期一,2 = 星期二,等等),然后从中减去 1 -> 那就是我们需要从当前日期减去多少天才能回到 weekday
值 1(星期一)。
下面是一个可以执行此操作的函数:
Public Function DatePrevWeekday( _
ByVal datDate As Date, _
Optional ByVal bytWeekday As VbDayOfWeek = vbMonday) _
As Date
' Returns the date of the previous weekday, as spelled in vbXxxxday, prior to datDate.
' 2000-09-06. Cactus Data ApS.
' No special error handling.
On Error Resume Next
DatePrevWeekday = DateAdd("d", 1 - Weekday(datDate, bytWeekday), datDate)
End Function
由于 vbMonday 是 2 而你的日期是今天,你可以在查询中使用核心表达式:
PreviousMonday: DateAdd("d",1-Weekday(Date(),2),Date())
我需要使我的 Access 查询始终 return 在本周的星期一。我在 Google/Whosebug 上看到了一些解决方案,但它们是用 SQL 编写的,我是创建 Access 查询的初学者(我正在使用设计视图来创建它们)。
目标:周应被视为 M T W T F S S。 然后,查询应始终 return 当前周的星期一。因此,如果是星期天,应该还是 return 前一个星期一, 而不是 下周的星期一。谁能解释如何使用 Access 2010 中的设计视图执行此操作?
请记住,在这种情况下,我们使用的是日期,因此如果我们这样做 Date() - 1
,我们将得到今天之前的 1 天。
Date()
~ 今天日期
DatePart(
"w" - Weekday
Date() - Today's date
2 - vBMonday (Access assumes Sunday is the first day of the week, which is why this is necessary.)
1 - vbFirstJan1 - This gets into using the first week of the year. We could have omitted this, as 1 is the default.
)
-1 - Subtract 1 from the DatePart value.
值
Date() = 4/27/2015 (at time of this writing)
DatePart("w",Date(),2,1) = 1
DatePart("w",Date(),2,1)-1 = 0
所以我们有 Date()-0
...好吧,这有什么了不起的?好吧,让我们看一个更有用的场景,今天的日期不是星期一。
假设今天是 4/28/2015(星期二)
Date() = 4/28/2015
DatePart("w",Date(),2,1) = 2
DatePart("w",Date(),2,1)-1 = 1
所以,从外到内;给我当前的工作日值。 (1 = 星期一,2 = 星期二,等等),然后从中减去 1 -> 那就是我们需要从当前日期减去多少天才能回到 weekday
值 1(星期一)。
下面是一个可以执行此操作的函数:
Public Function DatePrevWeekday( _
ByVal datDate As Date, _
Optional ByVal bytWeekday As VbDayOfWeek = vbMonday) _
As Date
' Returns the date of the previous weekday, as spelled in vbXxxxday, prior to datDate.
' 2000-09-06. Cactus Data ApS.
' No special error handling.
On Error Resume Next
DatePrevWeekday = DateAdd("d", 1 - Weekday(datDate, bytWeekday), datDate)
End Function
由于 vbMonday 是 2 而你的日期是今天,你可以在查询中使用核心表达式:
PreviousMonday: DateAdd("d",1-Weekday(Date(),2),Date())