|
-
Sep 14th, 2005, 09:32 AM
#1
Thread Starter
Hyperactive Member
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
-
Sep 14th, 2005, 09:41 AM
#2
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
Last edited by Mark Gambo; Sep 14th, 2005 at 09:55 AM.
Regards,
Mark
Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."
-
Sep 14th, 2005, 09:48 AM
#3
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
-
Sep 14th, 2005, 10:19 AM
#4
Thread Starter
Hyperactive Member
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?
-
Sep 14th, 2005, 10:32 AM
#5
Re: Populate combo box with Mondays date from given date?
Not sure what you mean ...
-
Sep 14th, 2005, 10:37 AM
#6
Thread Starter
Hyperactive Member
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
-
Sep 14th, 2005, 11:10 AM
#7
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|