Populate combo box with Mondays date from given date?
Hi,
I want to populate a combo box with the Monday's dates from a given date, which the user can select, but not sure where to start?
e.g.
Stored date = 13/05/2005
The combo box then has listed: -
16/05/2005
23/05/2005
30/05/2005
06/06/2005
etc.
etc.
Any ideas would be appreciated.
Regards
Re: Populate combo box with Mondays date from given date?
If you know the date of the first monday you could use:
VB Code:
Dim dtMonday as Date
dtMonday="12/9/2005" ' This date has to be a monday
Me.Combo1.AddItem dtMonday
For a = 1 to 25 'This will add the next 25 Monday's Date
Me.Combo1.AddItem DateAdd("ww", a, dtMonday) '<==== Edited Line
Next a
Re: Populate combo box with Mondays date from given date?
Would the following work for you?
VB Code:
Option Explicit
Private Sub Command1_Click()
GetMondays Date, Combo1, 5
End Sub
Public Sub GetMondays(dt As Date, cbo As ComboBox, num As Integer)
'==================================================================
Dim dtMonday As Date, iDay%, i%
iDay = Weekday(dt, vbMonday)
If Not iDay = 1 Then
dtMonday = DateAdd("d", -(iDay - 1), dt)
Else
dtMonday = dt
End If
For i = 1 To num
cbo.AddItem Format(DateAdd("d", i * 7, dtMonday), "mm/dd/yyyy")
Next i
End Sub
Re: Populate combo box with Mondays date from given date?
Thanks to you both for your replies.
Just a thought....
Instead of having to select the correct date everytime would there be a way to check to days date and have the focus of the combo on the apllicable monday?
Re: Populate combo box with Mondays date from given date?
Not sure what you mean ... :confused:
Re: Populate combo box with Mondays date from given date?
I'll have stored a start date, this date represent the start of the accounting year i.e. 13/05/2005.
Using your example I'll pass this date plus 52 to populate the combo box with the Mondays for the year.
Once populated I want to check todays date e.g. 14/09/2005 then have the combo box have the applicable Mondays date selected e.g. 12/09/2005
Hope I've explained myself a bit better?
Regards
Re: Populate combo box with Mondays date from given date?
Oh, ok see modified version:
VB Code:
Public Sub GetMondays(dt As Date, cbo As ComboBox, num As Integer)
'==================================================================
Dim dtMonday As Date, iDay%, i%
iDay = Weekday(dt, vbMonday)
If Not iDay = 1 Then
dtMonday = DateAdd("d", -(iDay - 1), dt)
Else
dtMonday = dt
End If
For i = 0 To num
cbo.AddItem Format(DateAdd("d", i * 7, dtMonday), "mm/dd/yyyy")
Next i
'find current week
iDay = Weekday(Date, vbMonday)
If Not iDay = 1 Then
dtMonday = DateAdd("d", -(iDay - 1), Date)
Else
dtMonday = Date
End If
For i = 0 To cbo.ListCount - 1
If cbo.List(i) = Format(dtMonday, "mm/dd/yyyy") Then
cbo.ListIndex = i
Exit For
End If
Next i
End Sub