2016-05-25 2 views
0

私は仕事日を計算する公開関数(実際には2つ一緒に働く)を持っています。それは私のために正常に動作しますが、他の人は0を返します。私はなぜ自分が狂っているのか理解しようとしていますが、なぜこれが起こりますか?彼らは月のために、彼らは21を取得する必要がありますし、私は、エラーを得ることはありません...しかし、彼らはただ公共の関数は私以外のすべての人に0を返します(アクセス2010)

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 
+0

誰もが同じデータベースを使用していますか?最初の関数のエラートラップからExitを削除し、エラーが発生するかどうかを確認することをお勧めします。または、その出口の前に次の行を追加しないようにしてください:workdays = 9876そして、その番号を取得するかどうかを確認してください。 –

答えて

0

0は、ユーザーのPCの日付形式を使用してRegeional設定を比較し得ます。

コントロールパネルの下に|地域と言語 - 短い日付と長い日付の形式を比較し、それらがあなたのものと同じかどうかを確認してください

関連する問題