公共函数为除我之外的其他人返回0(Access 2010)

时间:2016-05-25 16:37:09

标签: function ms-access-2010

我有一个计算工作日的公共功能(实际上是2个一起工作)。它适用于ME,但其他人都返回0。我正在疯狂地试图找出原因,为什么会发生这种情况?他们没有得到错误,因为五月,他们应该得到21而我做...但他们只得到0。

Public Function Workdays(ByRef StartDate As Date, _
 ByRef EndDate As Date, _
 Optional ByRef strHolidays As String = "dbo_tblHolidays" _
 ) As Integer
' Returns the number of workdays between startDate
' and endDate inclusive.  Workdays excludes weekends and
' holidays. Optionally, pass this function the name of a table
' or query as the third argument. If you don't the default
' is "dbo_tblHolidays".
On Error GoTo Workdays_Error
Dim nWeekdays As Integer
Dim nHolidays As Integer
Dim strWhere As String

' DateValue returns the date part only.
StartDate = DateValue(StartDate)
EndDate = DateValue(EndDate)

nWeekdays = Weekdays(StartDate, EndDate)
If nWeekdays = -1 Then
    Workdays = -1
    GoTo Workdays_Exit
End If

strWhere = "[fldHolidayDate] >= #" & StartDate _
    & "# AND [fldHolidayDate] <= #" & EndDate & "#"

' Count the number of holidays.
nHolidays = DCount(Expr:="[fldHolidayDate]", _
    Domain:=strHolidays, _
    Criteria:=strWhere)

Workdays = nWeekdays - nHolidays

Workdays_Exit:
    Exit Function

Workdays_Error:
    Resume Workdays_Exit
    Workdays = -1
    MsgBox "Error " & Err.Number & ": " & Err.Description, _
    vbCritical, "Workdays"
Resume Workdays_Exit

End Function

Public Function Weekdays(ByRef StartDate As Date, _
ByRef EndDate As Date _
) As Integer
' Returns the number of weekdays in the period from startDate
' to endDate inclusive. Returns -1 if an error occurs.
' If your weekend days do not include Saturday and Sunday and
' do not total two per week in number, this function will
' require modification.
On Error GoTo Weekdays_Error

' The number of weekend days per week.
Const ncNumberOfWeekendDays As Integer = 2

' The number of days inclusive.
Dim varDays As Variant

' The number of weekend days.
Dim varWeekendDays As Variant

' Temporary storage for datetime.
Dim dtmX As Date

' If the end date is earlier, swap the dates.
If EndDate < StartDate Then
    dtmX = StartDate
    StartDate = EndDate
    EndDate = dtmX
End If

' Calculate the number of days inclusive (+ 1 is to add back startDate).
varDays = DateDiff(Interval:="d", _
    date1:=StartDate, _
    date2:=EndDate) + 1

' Calculate the number of weekend days.
varWeekendDays = (DateDiff(Interval:="ww", _
    date1:=StartDate, _
    date2:=EndDate) _
    * ncNumberOfWeekendDays) _
    + IIf(DatePart(Interval:="w", _
    Date:=StartDate) = vbSunday, 1, 0) _
    + IIf(DatePart(Interval:="w", _
    Date:=EndDate) = vbSaturday, 1, 0)

' Calculate the number of weekdays.
Weekdays = (varDays - varWeekendDays)

Weekdays_Exit:
Exit Function

Weekdays_Error:
Weekdays = -1
MsgBox "Error " & Err.Number & ": " & Err.Description, _
    vbCritical, "Weekdays"
Resume Weekdays_Exit
End Function

1 个答案:

答案 0 :(得分:0)

将您的Regeional设置与用户PC的日期格式进行比较。

在“控制面板”下区域和语言 - 比较短日期和长日期格式,看看它们是否与您的相同