Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Finding first Monday of the month

Status
Not open for further replies.

henley12

Programmer
Mar 26, 2004
17
0
0
US
I have a form with 2 combo boxes, a date field and a command button. The first combo box has Every first or Every last. The second combo box has the days of the week. Given that I select Every first and Monday, how would I make the command button populate the date field with the chosen criteria? For instance, based on today's date, if I select Every first in the first combo box and Wednesday in the second, when I click the button, I want the date field to display 10/3/2007, which is the first Wednesday in October. On every subsequent click of the button, it would need to display the first Wednesday of the next month, and so on. Any ideas?
 
I did not write this one, but it was in my date function library. You could tweak it to work for any first day by also passing in the weeday you are looking for and using a select case
Code:
Function 1stMonday(dtmDate)
    '
    'Warning dtmDate must be a valid date
    '
    'Return the First Monday of provided month
    '
    Dim wrkDate    As Variant
    Dim intMonth   As Integer
    Dim intYear    As Integer
    Dim intDay     As Integer
    Dim intDayCnt  As Integer
    Dim intDiff    As Integer
    
    intMonth = Month(dtmDate)
    intYear = Year(dtmDate)
    
    'get first date in month
    wrkDate = DateSerial(intYear, intMonth, 1)
  
    'get the day of week first day is on
    intDayCnt = Weekday(wrkDate, 1)
    
    'compute number of days to add to next monday
    If intDayCnt <> 2 Then
        If intDayCnt = 1 Then
            intDiff = 1
        Else
            intDiff = (7 - intDayCnt + 2)
        End If
    Else
        intDiff = 0
    End If
   
    wrkDate = DateAdd("d", intDiff, wrkDate)
    
    tb1stMonday = wrkDate

End Function
 
This is one that I wrote and I think it will find any occurence of any day.

Code:
Public Function getDayOccurence(dtmDate As Date) As Integer
  getDayOccurence = ((Day(dtmDate) - 1) \ 7) + 1
End Function

Public Function getXOccurenceDay(intOccurence As Integer, strDay As String, intMonth As Integer, intYear As Integer) As Date
  Dim tempDay As Date
  tempDay = DateSerial(intYear, intMonth, 1)
  
  Do Until ((getDayOccurence(tempDay) = intOccurence) And (WeekdayName(Weekday(tempDay)) = strDay)) Or (Month(tempDay) > intMonth)
    tempDay = tempDay + 1
  Loop
  If Month(tempDay) > intMonth Then
    MsgBox "There is not an " & intOccurence & " of this day in the given month. Returning the 4th Occurence"
    tempDay = getXOccurenceDay(4, strDay, intMonth, intYear)
  End If
  getXOccurenceDay = tempDay
End Function

so to find the 3rd Sunday in October 2007 then

getXOccurenceDay(3, "Sunday", 10, 2007)
I think it works for all possibilities, but double check.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top