Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2014-01-09
14
Medium Priority
?
514 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
  • 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
Industry Leaders: 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 52

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 52

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

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

Question has a verified solution.

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

Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
The viewer will learn how to  create a slide that will launch other presentations in Microsoft PowerPoint. In the finished slide, each item launches a new PowerPoint presentation and when each is finished it automatically comes back to this slide: …
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…

916 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