Page 1 of 2 12 LastLast
Results 1 to 15 of 23
  1. #1
    boboivan is offline Advanced Beginner
    Windows 7 32bit Access 2013
    Join Date
    Dec 2015
    Posts
    65

    Networkdays VBA works only for english date format

    Hi everybody!



    I use this code (taken from internet) to calculate net working days, but apparently works only for english(us) date format.

    Code:
    Public Function Workdays(ByRef startDate As Date, ByRef endDate As Date, Optional ByRef strHolidays As String = "Holidays") As Integer    
    
        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 = "[Holiday] >= #" & startDate & "# AND [Holiday] <= #" & endDate & "#"
        
        ' Count the number of holidays.
        nHolidays = DCount(Expr:="[Holiday]", Domain:=strHolidays, Criteria:=strWhere)
        
        Workdays = nWeekdays - nHolidays
        
    Workdays_Exit:
        Exit Function
        
    Workdays_Error:
        Workdays = -1
        MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical, "Workdays"
        Resume Workdays_Exit
        
    End Function
    Is there any way to make it work for every date format?

    Thx!

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,524
    If STartdate and EndDate variables were in date format to begin with (date =numeric), it may work with Format(date,'mm/dd/yy').
    but this code is converting from string so,
    convert your string into US date first, then back to EU.
    startDate = mid(date,4,2) & "/" & left(date,2) & "/" & right(date,4)

  3. #3
    boboivan is offline Advanced Beginner
    Windows 7 32bit Access 2013
    Join Date
    Dec 2015
    Posts
    65
    Thank you for your quick answer!

    Yes, the code works with format date mm/dd/yy. I need it to make it work with format dd/mmmm/yy (other's country format)

    I'm not very good with programming, so you've lost me.
    This is not my code, I took it from internet.
    Can you please give me some more details?

  4. #4
    orange's Avatar
    orange is online now Moderator
    Windows 8 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    You may get some ideas from this article on dates by Allen Browne.
    I also think that Ranman's routine to modify the format should do what you need.
    Good luck.

  5. #5
    boboivan is offline Advanced Beginner
    Windows 7 32bit Access 2013
    Join Date
    Dec 2015
    Posts
    65
    Thx Orange!

    I'd like to do what Ranman suggested, but I have no idea what exactly (step by step) to do.

  6. #6
    orange's Avatar
    orange is online now Moderator
    Windows 8 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    I tried the function you posted. It errors when trying to execute
    Weekdays(startDate, endDate). Function was not found. This indicates (to me anyway) that was some additional code you didn't post.

    Perhaps you could review what you have and repost.

  7. #7
    orange's Avatar
    orange is online now Moderator
    Windows 8 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    I found a function at
    https://groups.google.com/forum/#!to...al/Dd6HV--sH-4
    and modified it to deal with your formatted dates which are text /strings.

    Code:
    '---------------------------------------------------------------------------------------
    ' Procedure : BusinessDateDiffSpecial
    ' Author    : mellon
    ' Date      : 26/01/2016
    ' Purpose   : This is a derivative of the function BusinessDateDiff posted at
    '  https://groups.google.com/forum/#!topic/microsoft.public.inetserver.asp.general/Dd6HV--sH-4
    '
    'This revised function expects input of 2 text strings representiing dates in dd/mmmm/yy  format.
    'The start date is expected to be earlier than the end date.
    ' Typical parameter format:  "21/January/16"
    'The function returns the number of business/workdays between the entered dates.
    '
    'NOTE: This function does NOT deal with Holidays !!!!!!!!!!!
    '---------------------------------------------------------------------------------------
    '
    Public Function BusinessDateDiffSpecial(tDate1 As String, tDate2 As String) As Variant
    
        Dim iWeekday1
        Dim iWeekday2
        Dim lDateDiff
        Dim lWeeks
        Dim dTemp
        Dim date1 As Date
        Dim date2 As Date
        'convert string values to dates
        date1 = CDate(tDate1)
        date2 = CDate(tDate2)
    
        If Not IsDate(date1) Then GoTo BusinessDateDiffSpecial_Error
        If Not IsDate(date2) Then GoTo BusinessDateDiffSpecial_Error
        On Error GoTo BusinessDateDiffSpecial_Error
        ' if dates are reversed ed End is before Start, then reverse the values
        If date1 > date2 Then
            dTemp = date1
            date1 = date2
            date2 = dTemp
        End If
    
        iWeekday1 = WeekDay(date1, vbMonday) - 1
        iWeekday2 = WeekDay(date2, vbMonday) - 1
        If iWeekday1 > 4 Then iWeekday1 = 4
        If iWeekday2 > 4 Then iWeekday2 = 4
    
        lWeeks = DateDiff("w", date1, date2)
    
        If iWeekday1 <= iWeekday2 Then
            lDateDiff = (lWeeks * 5) + (iWeekday2 - iWeekday1)
        Else
            lDateDiff = ((lWeeks + 1) * 5) - (iWeekday1 - iWeekday2)
    
        End If
    
        BusinessDateDiffSpecial = lDateDiff
    
        On Error GoTo 0
        Exit Function
    
    BusinessDateDiffSpecial_Error:
    
        MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure BusinessDateDiffSpecial of Module ModuleTesting_CanKill"
    
    End Function
    Test routine follows:
    Code:
    '---------------------------------------------------------------------------------------
    ' Procedure : testBusDays
    ' Author    : mellon
    ' Date      : 26/01/2016
    ' Purpose   :test routine
    '---------------------------------------------------------------------------------------
    '
    Sub testBusDays()
    
    Dim sdate As String
    Dim edate As String
    Dim StartDate As Date
    Dim Enddate As Date
    
       On Error GoTo testBusDays_Error
    
    sdate = "1/January/16"   'dd/mmmm/yy
    edate = "31/january/16"
    StartDate = CDate(sdate)
    Enddate = CDate(edate)
    Debug.Print BusinessDateDiffSpecial(sdate, edate)
    
       On Error GoTo 0
       Exit Sub
    
    testBusDays_Error:
    
        MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure testBusDays of Module ModuleTesting_CanKill"
    End Sub

    Good luck.

  8. #8
    boboivan is offline Advanced Beginner
    Windows 7 32bit Access 2013
    Join Date
    Dec 2015
    Posts
    65
    Quote Originally Posted by orange View Post
    I tried the function you posted. It errors when trying to execute
    Weekdays(startDate, endDate). Function was not found. This indicates (to me anyway) that was some additional code you didn't post.

    Perhaps you could review what you have and repost.
    You're right!
    The entire code is:
    Code:
    Option Compare Database
    Option Explicit
    
    Public Function workdays(ByRef startDate As Date, ByRef endDate As Date, Optional ByRef strHolidays As String = "Holidays") As Integer
     
        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 = "[Holiday] >= #" & startDate & "# AND [Holiday] <= #" & endDate & "#"
        
        ' Count the number of holidays.
        nHolidays = DCount(Expr:="[Holiday]", Domain:=strHolidays, Criteria:=strWhere)
        
        workdays = nWeekdays - nHolidays
        
    Workdays_Exit:
        Exit Function
        
    Workdays_Error:
        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
    Sorry for that! I didn't know it matters the other function

  9. #9
    boboivan is offline Advanced Beginner
    Windows 7 32bit Access 2013
    Join Date
    Dec 2015
    Posts
    65
    Thank you Orange!

    Your code doesn't take into account the Holidays (very important to me).
    Only for the working days I can use as calculated field (directly in the main table) the following formula:
    Code:
    ([DateEnd]-([DateStart]+(Weekday([DateEnd])-Weekday([DateStart]))))/7*5+(Weekday([DateEnd])-Weekday([DateStart]))+1+IIf(Weekday([DateEnd])=7,-1,0)+IIf(Weekday([DateStart])=1,-1,0)
    which works like a charm, but not enough for my needs.

  10. #10
    orange's Avatar
    orange is online now Moderator
    Windows 8 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    Well, I would suggest this
    works like a charm, but not enough for my needs.
    means you still don't have a solution.

    Do you have a holidays table? Can you show us enough info regarding your Dates and Holidays, so that we can mock up and test a solution?

  11. #11
    boboivan is offline Advanced Beginner
    Windows 7 32bit Access 2013
    Join Date
    Dec 2015
    Posts
    65
    Yes, I have a holiday table (like bellow) called "Holidays" with a column "Holiday" set up as date (a different date format than in the picture).


  12. #12
    orange's Avatar
    orange is online now Moderator
    Windows 8 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    with a column "Holiday" set up as date (a different date format than in the picture).
    I am guessing that Holiday is also formatted as dd/mmmm/yy ---Is that true???

  13. #13
    boboivan is offline Advanced Beginner
    Windows 7 32bit Access 2013
    Join Date
    Dec 2015
    Posts
    65
    yes, of course. All dates are in such format. I've change on my PC the date format from english(us) to my country's format. This is in fact the main problem. The code works only in the original format (english)

  14. #14
    orange's Avatar
    orange is online now Moderator
    Windows 8 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    I am sending you a revised function called WorkDaysT that allows you to input dates as text strings with your
    dd/mmmm/yy format. The holidays table in my test routine is called THolidays. Details/comments are included in the code below.

    The test routine calls WorkDaysT which in turn calls Weekdays. Weekdays has not been modified.
    I have tested it with 0,1,2 and 3 holiday dates. The results are what I expected.
    no holidays 20 workdays
    holiday 26Jan 19 workdays
    holiday 26Jan and 29Jan 18 workdays
    holiday 26Jan, 29Jan and 30Jan -17 workdays

    The logic of the workdays routine that you found does not consider a Holiday falling on a weekend???? If that is important, then additional testing/coding will be required.

    Here is the revised code

    Code:
    '---------------------------------------------------------------------------------------
    ' Procedure : workdaysT
    ' Author    : mellon
    ' Date      : 26/01/2016
    ' Purpose   : Pertains to
    'https://www.accessforums.net/access/networkdays-vba-works-only-english-date-format-57718.html#post308940
    '
    'This is a modification to the Workdays function to allow strings representing Dates in dd/mmmm/yy format
    '
    ' The strHolidays variable represents the name of the Holiday table. In this version  that name is THolidays.
    'I have made it the default name in the procedure call.
    '
    ' The startdate and enddate have been changed to text/strings instead of the original Date datatype
    'to accommodate your dd/mmmm/yy format.
    '---------------------------------------------------------------------------------------
    Public Function workdaysT(ByRef tstartDate As String, ByRef tendDate As String, _
                              Optional ByRef strHolidays As String = "THolidays") As Variant
     
      
        On Error GoTo workdays_Error
        Dim nWeekdays As Integer
        Dim nHolidays As Integer
        Dim strWhere As String
        Dim startDate As Date
        Dim endDate As Date
        
        'convert string to real dates '*****************************
        startDate = CDate(tstartDate)
        endDate = CDate(tendDate)
        
        ' DateValue returns the date part only.
        
        startDate = DateValue(startDate)
        endDate = DateValue(endDate)
        
        nWeekdays = Weekdays(startDate, endDate)
        If nWeekdays = -1 Then
            workdaysT = -1
            GoTo Workdays_Exit
        End If
        
        strWhere = "[Holiday] >= #" & startDate & "# AND [Holiday] <= #" & endDate & "#"
        
        ' Count the number of holidays.
        '
        'Note - This function needs to know the name of the Holidays table and
        '       the name of the field containing the Holiday date in dd/mmmm/yy format.
        
        nHolidays = DCount(expr:="[Holiday]", domain:=strHolidays, criteria:=strWhere)
        workdaysT = nWeekdays - nHolidays
        
    Workdays_Exit:
        Exit Function
        
    workdays_Error:
        workdaysT = -1
        MsgBox "Error " & Err.Number & ": " & Err.Description & " in procedure workdays of Module ModuleTesting_CanKill" _
        , vbCritical, "WorkdaysT"
        Resume Workdays_Exit
    
       On Error GoTo 0
       Exit Function
        
    End Function

    Weekdays (code unchanged)
    Code:
    '---------------------------------------------------------------------------------------
    ' Procedure : Weekdays
    ' Author    : mellon
    ' Date      : 26/01/2016
    ' Purpose   : Note, this function is called by WorkDaysT, after the strings have been converted to Dates.
    'So there is no need to modify the function parameters.
    '---------------------------------------------------------------------------------------
    '
    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
    Test routine to find workdays between 3/January/16 and 31/January/16 using the fictitious THolidays table and values.
    Code:
    '---------------------------------------------------------------------------------------
    ' Procedure : TestModifiedWorkdays
    ' Author    : mellon
    ' Date      : 26/01/2016
    ' Purpose   :Routine to test the WorkDaysT function.
    ' I have mocked up a Holiday table called THolidays.
    ' table_name     field_name
    '   tHolidays       ID          Long
    '   tHolidays       Description Text
    '   tHolidays       Holiday     Text    255  Dates in dd/mmmm/yy format
    '
    'The Holidays(fictitious) for this test routine are
    '
    'ID  Description Holiday
    '1   MadeUpHoliday "26/january/16"
    '2   AnotherHoliday "29/january/16"
    '3   holidayOnWeekend "30/january/16"
    '
    ' I am looking for workdays between 3/January/16 and 31/january/16
    '---------------------------------------------------------------------------------------
    '
    Sub TestModifiedWorkdays()
    
    Dim d1 As String
    Dim d2 As String
       On Error GoTo TestModifiedWorkdays_Error
    
    d1 = "3/january/16"
    d2 = "31/January/16"
    Debug.Print workdaysT(d1, d2)
    
       On Error GoTo 0
       Exit Sub
    
    TestModifiedWorkdays_Error:
    
        MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure TestModifiedWorkdays of Module ModuleTesting_CanKill"
    End Sub

  15. #15
    boboivan is offline Advanced Beginner
    Windows 7 32bit Access 2013
    Join Date
    Dec 2015
    Posts
    65
    It returns the same error.
    I don't know what am I doing wrong.
    I've copied the workdaysT function over the old version workdays. I've renamed THolidays to Holidays (as my table name) and workdaysT to workdays.
    I don't know what that test routine does and what should I do with it

    Please advice!

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. English <==> American Date conversion
    By tb1150 in forum Access
    Replies: 1
    Last Post: 06-22-2015, 05:19 AM
  2. Date return from NetworkDays and Holidays
    By Paintballlovr in forum Queries
    Replies: 9
    Last Post: 04-01-2015, 10:13 AM
  3. Replies: 4
    Last Post: 03-12-2013, 06:49 PM
  4. Adding to a date switches from italian to english format
    By marcotognon in forum Programming
    Replies: 4
    Last Post: 06-24-2011, 07:46 AM
  5. Replies: 1
    Last Post: 01-21-2011, 11:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums