首先的问题是,我有一张包含两个日期的表格,我正在尝试创建一个函数,我可以传递这两个日期来计算这两个日期之间的工作日数
我有另一个表,其中包含非工作日列表(例如当地假日等),该表称为 [CNNWDATE]。
其结构如下。
新日期 新日期 DNDAY
2002 年 3 月 29 日
2002 年 1 月 4 日
2002 年 2 月 4 日
2002 年 6 月 5 日
这些表的结构是由另一个软件创建的。它们的结构无法更改,我无法控制它们。它们是来自 Oracle 数据库的链接表
我发现这个链接对于我所追求的是一个很好的开始http://www.databasedev.co.uk/calculate_work_days.html 并创建了以下函数
Public Function Work_Days(BegDate As Date, EndDate As Date)
'calculate number of working days between to dates
'note this relys on CNNWDATE having been completed with non working days marked
Dim intNMB_NonW_Days As Integer
Dim WholeWeeks As Variant
Dim DateCnt As Variant
Dim EndDays As Integer
On Error GoTo Err_Work_Days
BegDate = DateValue(BegDate)
EndDate = DateValue(EndDate)
WholeWeeks = DateDiff("w", BegDate, EndDate)
DateCnt = DateAdd("ww", WholeWeeks, BegDate)
EndDays = 0
intNMB_NonW_Days = 0
'count cnnwdate btween dates
intNMB_NonW_Days = DCount("*", "[CNNWDATE]", "[CNNWDATE.NWDATE] >= _
BegDate and [CNNWDATE.NWDATE] <= Enddate")
Do While DateCnt <= EndDate
If Format(DateCnt, "ddd") <> "Sun" And _
Format(DateCnt, "ddd") <> "Sat" Then
EndDays = EndDays + 1
End If
DateCnt = DateAdd("d", 1, DateCnt)
Loop
Work_Days = WholeWeeks * 5 + EndDays - intNMB_NonW_Days
Exit Function
Err_Work_Days:
If Err.Number = 94 Then
Work_Days = 0
Exit Function
Else
MsgBox "Error " & Err.Number & ": " & Err.Description
End If
End Function
如果我注释掉 DCOUNT,我会得到工作日的数量,但我无法让 DCOUNT 在 CNNWDATE 中实际返回非工作日的数量
我得到的错误是
错误 2766:该对象不包含自动化对象“BegDate”
任何帮助我哪里做错了,表示感谢
答案1
我感觉现在发布这个帖子真的很愚蠢,因为我偶然发现了这篇关于 DCOUNT 的帖子https://bytes.com/topic/access/answers/963055-object-doesnt-contain-automation-object
并首先创建要传递给 DCOUNT 的字符串,然后传递它已经解决了问题,所以我的代码现在读取
Public Function Work_Days(BegDate As Date, EndDate As Date)
'calculate number of working days between to dates
'note this relys on CNNWDATE haveing been completed with non working days marked
Dim intNMB_NonW_Days As Integer
Dim WholeWeeks As Variant
Dim DateCnt As Variant
Dim EndDays As Integer
Dim sSQL As String
On Error GoTo Err_Work_Days
BegDate = DateValue(BegDate)
EndDate = DateValue(EndDate)
WholeWeeks = DateDiff("w", BegDate, EndDate)
DateCnt = DateAdd("ww", WholeWeeks, BegDate)
EndDays = 0
intNMB_NonW_Days = 0
'count cnnwdate between dates
sSQL = "[NWDATE] >=#" & CDate(BegDate) & "# and [NWDATE] <=#" & CDate(EndDate) & "#"
intNMB_NonW_Days = DCount("*", "[CNNWDATE]", sSQL)
Do While DateCnt <= EndDate
If Format(DateCnt, "ddd") <> "Sun" And _
Format(DateCnt, "ddd") <> "Sat" Then
EndDays = EndDays + 1
End If
DateCnt = DateAdd("d", 1, DateCnt)
Loop
Work_Days = WholeWeeks * 5 + EndDays - intNMB_NonW_Days
Exit Function
Err_Work_Days:
If Err.Number = 94 Then
Work_Days = 0
Exit Function
Else
MsgBox "Error " & Err.Number & ": " & Err.Description
End If
End Function