访问2个组合框的设置值到上周六和上周日

Access set value of 2 combo boxes to last Saturday and last Sunday

我想创建一个按钮,用于在表单 [start_date] 和 [end_date] 上设置控件。我想 [start_date] = 上周周日 | [end_date] = 上周周六。我知道 VBA 和宏,但我无法理解这个的逻辑。

您可以使用这个通用函数:

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

像这样:

Command268_Click() 
    Me![start_date].Value = DatePrevWeekday(Date, vbSunday)
    Me![end_date].Value = DatePrevWeekday(Date, vbSaturday)
End Sub 

谢谢古斯塔夫。有效。我做了一个小的功能改变,让它做我想做的事。

 Private Sub Command_SetWeekClosures_Click()
 Me![StartingDate_TextBox].Value = DatePrevWeekday(Date - 7, vbSunday)
 Me![EndingDate_TextBox].Value = DatePrevWeekday(Date, vbSaturday)
 End Sub

现在您将函数作为模块的一部分,

  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

该按钮将我的文本框设置为与上周的日期范围完全一致。