Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Find 3rd working day of the following month for a given date

Posted on 2014-01-09
14
Medium Priority
?
500 Views
Last Modified: 2014-01-10
I need to calculate three (3) working (or week) days following the last day of the month for a given date. For example, if WEDate = 1/3/14, the result should be 2/5/14 (3rd working (or week) day of the following month. (I'm using the following to return the last day of WEDate's month: LDOM: DateSerial(Year([WEDate]),Month([WEDate])+1,0). Any help is greatly appreciated. Thank you.
0
Comment
Question by:jaguar5554
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
  • 2
14 Comments
 
LVL 26

Expert Comment

by:jerryb30
ID: 39769076
Need we take holidays into account?
0
 

Author Comment

by:jaguar5554
ID: 39769082
Yes -- my apologies for not including that important detail!
0
 
LVL 26

Expert Comment

by:jerryb30
ID: 39769115
US Holidays? Any year? As far I can tell tell, only New Year's Day, Independence Day and Labor day could fall within the first week, but with multiple years, you still have a multitude of possible holidays.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:jaguar5554
ID: 39769127
US Holidays and current year; that is, current year of the WEDate. Specifically, I only need to see the 3rd working (or week) day of the month following the WEDate being evaluated. Clear as mud? :-)
0
 
LVL 26

Expert Comment

by:jerryb30
ID: 39769141
Not really. A date in December will go over to the next year.
You might need a table of US Holidays by year. Which could be as few as 3 dates or as many as you have years of data times three.
0
 

Author Comment

by:jaguar5554
ID: 39769277
I see. I'm assuming there is no way for the DateSerial function to specify a day v. weekday?
0
 
LVL 26

Expert Comment

by:jerryb30
ID: 39769634
No.  I have a partial solution, but there is a bug which I have to mull over at the local tavern.
0
 
LVL 51

Accepted Solution

by:
Gustav Brock earned 2000 total points
ID: 39770603
You can use the simple looping function below:

datDateFrom = DateSerial(Year(datDate), Month(datDate) + 1, 1)
datDateNext = DateAddWorkdays(datDateFrom, 3)

Public Function DateAddWorkdays(ByVal datDate As Date, Optional ByVal intWorkdays = 1) As Date

' Add intWorkdays to datDate ignoring weekends and holidays.
'
' 2010-10-07, Cactus Data ApS, CPH

    Dim dbs         As DAO.Database
    Dim rst         As DAO.Recordset
    
    Dim intDays     As Integer
    Dim strDate     As String
    Dim strSQL      As String
    
    strDate = Format(datDate, "yyyy\/mm\/dd")
    
    Set dbs = CurrentDb
    strSQL = "Select Top " & intWorkdays & " HolidayDate From tblHoliday Where HolidayDate >= #" & strDate & "#"
    Set rst = dbs.OpenRecordset(strSQL)
    
    While intDays < intWorkdays
        datDate = DateAdd("d", 1, datDate)
        intDays = intDays + 1
        ' Skip weekends.
        If Weekday(datDate, vbMonday) > Weekday(vbFriday, vbMonday) Then
            datDate = DateAdd("d", 1 + 7 - Weekday(datDate, vbMonday), datDate)
        End If
        ' Skip holidays.
        Do
            rst.FindFirst "HolidayDate = #" & Format(datDate, "yyyy\/mm\/dd") & "#"
            datDate = DateAdd("d", Abs(Not rst.NoMatch), datDate)
        Loop Until rst.NoMatch
    Wend
    
    rst.Close
    Set rst = Nothing
    Set dbs = Nothing
    
    DateAddWorkdays = datDate
    
End Function

Open in new window

Of course, adjust table and field name to those of yours.
/gustav
0
 

Author Comment

by:jaguar5554
ID: 39771246
That, gustav, is a beautiful thing! I should visit the tavern more often myself! LOL

I changed datDateNext = DateAddWorkdays(datDateFrom, 3)
to datDateNext = DateAddWorkdays(datDateFrom, 2) and on intial spot check am getting the exact results I'm looking for. All I need to do is update the tblHoliday table at the beginning of the new year. Genius. Thank you so much!
0
 
LVL 51

Expert Comment

by:Gustav Brock
ID: 39771254
Great! You are welcome.

/gustav
0
 
LVL 26

Expert Comment

by:jerryb30
ID: 39771264
Just to give credit-I visited the tavern. Did not come up with a solution. Gustav may or may not have visited a tavern.
0
 

Author Comment

by:jaguar5554
ID: 39771269
Scratch that! I changed it back to datDateNext = DateAddWorkdays(datDateFrom, 3)
(the data I was testing was entered for the wrong year!!). Your original expression works perfectly. Thank you Thank you again. Expert!
0
 

Author Closing Comment

by:jaguar5554
ID: 39771284
I combed the Internet and none of the solutions I found worked as expected. This solution is flawless. Gustav is an expert and generous in sharing his expertise.
0
 

Author Comment

by:jaguar5554
ID: 39771292
jerryb30 -- I hope you had an enjoyable visit at the pub.
gustav -- treat yourself to a visit at the pub.
:-)
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article helps those who get the 0xc004d307 error when trying to rearm (reset the license) Office 2013 in a Virtual Desktop Infrastructure (VDI) and/or those trying to prep the master image for Microsoft Key Management (KMS) activation. (i.e.- C…
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question