Results 1 to 2 of 2
  1. #1
    navensg is offline Novice
    Windows XP Access 2007
    Join Date
    May 2011
    Posts
    5

    Post Add hours to Date Value with working hours condition (Need help on existing Macro)

    Hello all,

    Need help on the existing macro, The below will add hours to particular datevalue with working hours

    ex: 7/18/2012 10:41 If you add 2 (means 2 hours) It will return 7/18/2012 12:41

    But the problem with this Macro:

    If Start Date is out side working hours like 7/20/2012 17:12 its giving 7/23/2012 10:12:00 AM, ideally it should give 7/23/2012 10:00:00 AM
    The same way if it is 7/19/2012 7:59 its giving 7/19/2012 10:59 it should give 7/19/2012 10:00


    Here is some sample data:

    Opened
    7/18/2012 10:41
    7/13/2012 8:48
    7/20/2012 16:26
    7/20/2012 17:12
    7/3/2012 13:50
    7/16/2012 15:36
    7/18/2012 12:00
    6/29/2012 13:29
    7/19/2012 7:59




    Code:
    Public Function WorkhourAdd( _
           ByVal datDateStart As Date, _
           ByVal intHours As Integer) _
           As Date
    ' Purpose: Add number of working hours to date datDateStart.
    ' Assumes: 5 working days per week. Adjust cbytWorkdaysOfWeek for other values.
    ' First workday is Monday.
    ' Weekend is up to and including Sunday.
    
    ' Specify begin and end time of daily working hours.
       Const cdatWorkTimeStart As Date = #8:00:00 AM#
       Const cdatWorkTimeStop As Date = #5:00:00 PM#
       Const cbytWorkdaysOfWeek As Byte = 5
       Dim intCount As Integer
       Dim datDateEnd As Date
       datDateEnd = datDateStart
       While intCount < intHours
          datDateEnd = DateAdd("h", 1, datDateEnd)
          If Weekday(datDateEnd, vbMonday) <= cbytWorkdaysOfWeek Then
             If DateDiff("h", cdatWorkTimeStart, TimeValue(datDateEnd)) > 0 Then
                If DateDiff("h", TimeValue(datDateEnd), cdatWorkTimeStop) >= 0 Then
                   intCount = intCount + 1
                End If
             End If
          End If
       Wend
       WorkhourAdd = datDateEnd
    End Function



    Thanks in Advance,
    Naveen
    Last edited by RuralGuy; 08-06-2012 at 11:21 AM. Reason: Added indenting and code tags

  2. #2
    jzwp11 is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    I think the following addition shown in red below should do what you are after

    Code:
    Public Function WorkhourAdd( _
           ByVal datDateStart As Date, _
           ByVal intHours As Integer) _
           As Date
    ' Purpose: Add number of working hours to date datDateStart.
    ' Assumes: 5 working days per week. Adjust cbytWorkdaysOfWeek for other values.
    ' First workday is Monday.
    ' Weekend is up to and including Sunday.
    ' Specify begin and end time of daily working hours.
       Const cdatWorkTimeStart As Date = #8:00:00 AM#
       Const cdatWorkTimeStop As Date = #5:00:00 PM#
       Const cbytWorkdaysOfWeek As Byte = 5
       Dim intCount As Integer
       Dim datDateEnd As Date
       datDateEnd = datDateStart
    
    
       'check to see if the stop time is past 5pm; if it is set it to 5pm
       If TimeValue(datDateEnd) > cdatWorkTimeStop Then
        datDateEnd = DateValue(datDateEnd) + cdatWorkTimeStop
       End If
       
       'check to see if the start time is before 8am; if it is set it to 8am
       If TimeValue(datDateEnd) < cdatWorkTimeStart Then
        datDateEnd = DateValue(datDateEnd) + cdatWorkTimeStart
       End If
    
    
    
       While intCount < intHours
          datDateEnd = DateAdd("h", 1, datDateEnd)
          If Weekday(datDateEnd, vbMonday) <= cbytWorkdaysOfWeek Then
             If DateDiff("h", cdatWorkTimeStart, TimeValue(datDateEnd)) > 0 Then
                If DateDiff("h", TimeValue(datDateEnd), cdatWorkTimeStop) >= 0 Then
                   intCount = intCount + 1
                End If
             End If
          End If
       Wend
       WorkhourAdd = datDateEnd
    End Function

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 14
    Last Post: 09-11-2013, 01:10 PM
  2. Replies: 4
    Last Post: 08-06-2012, 10:25 AM
  3. Can hours be sum?
    By newtoAccess in forum Queries
    Replies: 9
    Last Post: 10-10-2011, 11:01 PM
  4. Hours and OT Hours
    By Ruthers456 in forum Forms
    Replies: 2
    Last Post: 06-27-2011, 10:09 AM
  5. hours + condition
    By Miriam in forum Queries
    Replies: 0
    Last Post: 08-09-2009, 06:46 AM

Tags for this Thread

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