Results 1 to 15 of 15

Thread: [Resolved]Date plus 10 workdays

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2002
    Location
    Sunny Scottsdale Arizona
    Posts
    254

    Question [Resolved]Date plus 10 workdays

    I have a masked edit box which I am using to enter a date. Next to it I have a label.

    I am trying to come up with a formula to accomplish the following:

    Label1 = MaskEdBox1 + 10 workdays

    Ex. If the user entered 10/7/03 in MaskEdBox1, Label1 would then show 10/21/03 (the 11th, 12th, 18th & 19th are all weekend days)
    Last edited by Jefftopia; Oct 22nd, 2003 at 10:50 AM.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    the function you want is DateAdd.

    I haven't got VB help where I am at the moment, so you'll need to check the parameters (the order of them, and the string for working day - I think its "ww").

    Label1 = DateAdd("ww",MaskEdBox1, 10)

  3. #3
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    Here's one technique that you could adapt.

    VB Code:
    1. Dim dteNow As Date
    2.     Dim intWorkDays As Integer
    3.    
    4.     dteNow = Now
    5.     Do Until intWorkDays = 10
    6.         If Format(dteNow, "ddd") = "Sat" Or Format(dteNow, "ddd") = "Sun" Then
    7.             ' Don't count it
    8.         Else
    9.             intWorkDays = intWorkDays + 1
    10.         End If
    11.         dteNow = dteNow + 1
    12.     Loop
    13.    
    14.     MsgBox "10 workdays from today is " & dteNow

    How will you handle holidays?

  4. #4
    Frenzied Member Buzby's Avatar
    Join Date
    Jan 1999
    Location
    UK
    Posts
    1,670
    "ww" is for weeks
    "w" is for days
    "d" is for days

    As far as I know there is no easy way to work out Workdays.

    Try this function:

    VB Code:
    1. Public Function AddWorkDays(ThisDate As Date, ThisDays As Integer) As Date
    2. Dim tmpCount As Integer
    3. Dim CurDate As Date
    4. CurDate = ThisDate
    5. tmpCount = 0
    6. While tmpCount < ThisDays
    7.     CurDate = DateAdd("d", 1, CurDate)
    8.     If Weekday(CurDate) <> vbSaturday And Weekday(CurDate) <> vbSunday Then tmpCount = tmpCount + 1
    9. Wend
    10. AddWorkDays = CurDate
    11. End Function

    Then you can do

    Msgbox AddWorkDays("13-Oct-2003",10)

    which will return 27th Octob 2003.
    'Buzby'
    Visual Basic Developer
    "I'm moving to Theory. Everything works there."

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    Originally posted by Buzby
    "ww" is for weeks
    "w" is for days
    "d" is for days
    just checked VBA help - "w" is supposed to be for weekdays, but it does exactly the same as "d"

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jun 2002
    Location
    Sunny Scottsdale Arizona
    Posts
    254

    Smile

    Thank you all.

    DateAdd did not seem to take into consideration weekends when adding to the date I provided.

    Seemed that MartinLiss and Buzby were both basically on the same page. Ultimately I used Buzby's piece since it was already in function form. Works great. Thank you sirs.

  7. #7

  8. #8
    Frenzied Member Buzby's Avatar
    Join Date
    Jan 1999
    Location
    UK
    Posts
    1,670
    In order to take into accounts bank holidays etc you would need to have a table of holiday dates somewhere, and check each date inside the loop

    VB Code:
    1. Public Function AddWorkDays(ThisDate As Date, ThisDays As Integer) As Date
    2. Dim tmpCount As Integer
    3. Dim CurDate As Date
    4. CurDate = ThisDate
    5. tmpCount = 0
    6. While tmpCount < ThisDays
    7.     CurDate = DateAdd("d", 1, CurDate)
    8.     If Weekday(CurDate) <> vbSaturday And Weekday(CurDate) <> vbSunday [b]And Not IsHoliday(CurDate)[/b] Then tmpCount = tmpCount + 1
    9. Wend
    10. AddWorkDays = CurDate
    11. End Function

    Then you will need a IsHoliday function that takes the date and looks it up in the holidays table to decide whether that day is a holiday or not.
    'Buzby'
    Visual Basic Developer
    "I'm moving to Theory. Everything works there."

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Jun 2002
    Location
    Sunny Scottsdale Arizona
    Posts
    254
    Would be great to exclude holidays in the calculation. Wasn't sure if I wanted to go through the trouble at this time. If you had something I'd be happy to try it. Otherwise, I am getting pressed by the boss to get this done.

    thank you sir.

  10. #10
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    Here's an IsHoliday function. It hardcodes the dates but it would be better to read them from a database.

    VB Code:
    1. Public Function IsHoliday(dteDate As Date) As Boolean
    2.  
    3.     Select Case dteDate
    4.         Case "12/25/03", "01/01/04" ' etc.
    5.             IsHoliday = True
    6.         Case Else
    7.             IsHoliday = False
    8.     End Select
    9.  
    10. End Function

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Jun 2002
    Location
    Sunny Scottsdale Arizona
    Posts
    254

    Thumbs up

    thanks again. I think I'll use your short solution now and go back at a later date to draw holiday dates from database. You are awesome.

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Jun 2002
    Location
    Sunny Scottsdale Arizona
    Posts
    254
    I ended up making a holidays table in my database and altering IsHoliday function. Here is what I came up with. Works Great. Thanks again for all the help.

    VB Code:
    1. Public Function IsHoliday(dteDate As Date) As Boolean
    2.  
    3.     Dim MySQL As String
    4.     Dim MyDb As Database
    5.     Dim MySet As Recordset
    6.    
    7.     MySQL = "SELECT * FROM tblHolidays"
    8.    
    9.     Set MyDb = OpenDatabase(RDSDATA_PATH & "rdstables.mdb")
    10.     Set MySet = MyDb.OpenRecordset(MySQL)
    11.    
    12.     If MySet.RecordCount > 0 Then
    13.         Do Until MySet.EOF
    14.             If dteDate = MySet.Fields("Date") Then
    15.                 IsHoliday = True
    16.                 Exit Do
    17.             Else
    18.                 IsHoliday = False
    19.             End If
    20.             MySet.MoveNext
    21.         Loop
    22.     End If
    23.  
    24.     MySet.Close
    25.     MyDb.Close
    26.     Set MySet = Nothing
    27.     Set MyDb = Nothing
    28.  
    29. End Function

  13. #13
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    I added vbcode tags to your post to make it more readable. That's something you should almost always do when you post code.


    Don't do the SQL within the Function. Doing it that way entails a lot of overhead every time the function is called. A better solution would be to load an array with the dates at startup and have the IsHoliday function read the array. Here is untested code to do that.

    VB Code:
    1. Option Explicit
    2. Private dteHolidays() As Date
    3.  
    4. Private Sub Form_Load()
    5.  
    6.     Dim MySQL As String
    7.     Dim MyDb As Database
    8.     Dim MySet As Recordset
    9.    
    10.     MySQL = "SELECT * FROM tblHolidays"
    11.    
    12.     Set MyDb = OpenDatabase(RDSDATA_PATH & "rdstables.mdb")
    13.     Set MySet = MyDb.OpenRecordset(MySQL)
    14.    
    15.     If MySet.RecordCount > 0 Then
    16.         ReDim dteHolidays(MySet.RecordCount)
    17.         Do Until MySet.EOF
    18.             dteHolidays(UBound(dteHolidays)) = MySet.Fields("Date")
    19.             MySet.MoveNext
    20.         Loop
    21.     End If
    22.  
    23.     MySet.Close
    24.     MyDb.Close
    25.     Set MySet = Nothing
    26.     Set MyDb = Nothing
    27.  
    28. End Sub
    29. Public Function IsHoliday(dteDate As Date) As Boolean
    30.  
    31.     Dim intIndex As Integer
    32.    
    33.     For intIndex = 0 To UBound(dteHolidays) - 1
    34.         If dteDate = dteHolidays(intIndex) Then
    35.             IsHoliday = True
    36.             Exit Function
    37.         End If
    38.     Next
    39.     IsHoliday = False
    40.  
    41. End Function

  14. #14
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    One other thing you should consider doing is to compare the date in question to the latest date in the array, and if the date in question is greater issue a message like "Warning. Holiday list needs to be updated".

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Jun 2002
    Location
    Sunny Scottsdale Arizona
    Posts
    254

    Cool

    I like the way you are using UBound to load the array. I must admit that I have been mostly a one trick pony (not fully taken advantage of arrays). I am going to try and change that going forward. Also, liked your suggestion for the Warning message. I am going to institute that tomorrow.

    PS I will start tagging my code going forward.

    Thanks for your time.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width