以下 VBA 函数将允许您运行表单的 Access 查询
SELECT CountWeekendDays([from date], [to date]) AS WeekendDays FROM YourTable
只需在 Access 中新建一个Module
并将以下代码粘贴到其中:
Public Function CountWeekendDays(Date1 As Date, Date2 As Date) As Long
Dim StartDate As Date, EndDate As Date, _
WeekendDays As Long, i As Long
If Date1 > Date2 Then
StartDate = Date2
EndDate = Date1
Else
StartDate = Date1
EndDate = Date2
End If
WeekendDays = 0
For i = 0 To DateDiff("d", StartDate, EndDate)
Select Case Weekday(DateAdd("d", i, StartDate))
Case 1, 7
WeekendDays = WeekendDays + 1
End Select
Next
CountWeekendDays = WeekendDays
End Function