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

时间:2015-03-24 14:19:16

标签: ms-access access-vba

我想创建一个按钮,在窗体[start_date]和[end_date]上设置控件。我想[start_date] =上周星期日|和[end_date] =上周六。我知道VBA和宏,但我无法弄清楚这个逻辑。

2 个答案:

答案 0 :(得分:0)

您可以使用此通用功能:

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 

答案 1 :(得分:0)

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

 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

该按钮将我的文本框设置为上周的日期范围。