Link to home
Start Free TrialLog in
Avatar of swilli6926
swilli6926

asked on

Date Question

If I had a simple form with one textbox and a listbox and I wanted to type the year into the textbox (ex. 2004 or 2005 etc) and have the listbox populate with
all the dates "??/??/????" that start each week.  Like all the Sundays.  How would that be done?

Any Suggestions?

Avatar of S-Twilley
S-Twilley

Ok, this may need more testing, but this seems to work (areas that may need to be tested is when the 31/12/xxxx is a sunday)

Sub ListAllSundays(ByVal YearNum As Integer, Byval ListX as Listbox)
        Dim i As Integer
        Dim thisDate As Date
        Dim firstSunday As Integer
        For i = 1 To 7
            thisDate = New Date(YearNum , 1, i)
            If thisDate.DayOfWeek = DayOfWeek.Sunday Then
                Exit For
            End If
        Next

        Do While thisDate.Year = YearNum
            ListX.Items.Add(thisDate.ToShortDateString)
            thisDate = thisDate.AddDays(7)
        Loop
End Sub
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ok... Idle's is a lot neater than mine (and takes into account different days :P)