Results 1 to 7 of 7

Thread: Populate combo box with Mondays date from given date?

  1. #1

    Thread Starter
    Hyperactive Member Pozzi's Avatar
    Join Date
    Feb 2001
    Location
    The Stones!
    Posts
    507

    Question 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
    VB.Net (VS 2010)

  2. #2
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: Populate combo box with Mondays date from given date?

    If you know the date of the first monday you could use:

    VB Code:
    1. Dim dtMonday as Date
    2.  
    3. dtMonday="12/9/2005" ' This date has to be a monday
    4. Me.Combo1.AddItem dtMonday
    5.  
    6. For a = 1 to 25 'This will add the next 25 Monday's Date
    7.     Me.Combo1.AddItem DateAdd("ww", a, dtMonday) '<==== Edited Line
    8. 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."


  3. #3
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Populate combo box with Mondays date from given date?

    Would the following work for you?
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4.     GetMondays Date, Combo1, 5
    5. End Sub
    6.  
    7. Public Sub GetMondays(dt As Date, cbo As ComboBox, num As Integer)
    8. '==================================================================
    9. Dim dtMonday As Date, iDay%, i%
    10.  
    11.     iDay = Weekday(dt, vbMonday)
    12.     If Not iDay = 1 Then
    13.         dtMonday = DateAdd("d", -(iDay - 1), dt)
    14.     Else
    15.         dtMonday = dt
    16.     End If
    17.    
    18.     For i = 1 To num
    19.         cbo.AddItem Format(DateAdd("d", i * 7, dtMonday), "mm/dd/yyyy")
    20.     Next i
    21.  
    22. End Sub

  4. #4

    Thread Starter
    Hyperactive Member Pozzi's Avatar
    Join Date
    Feb 2001
    Location
    The Stones!
    Posts
    507

    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?
    VB.Net (VS 2010)

  5. #5

  6. #6

    Thread Starter
    Hyperactive Member Pozzi's Avatar
    Join Date
    Feb 2001
    Location
    The Stones!
    Posts
    507

    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
    VB.Net (VS 2010)

  7. #7
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Populate combo box with Mondays date from given date?

    Oh, ok see modified version:
    VB Code:
    1. Public Sub GetMondays(dt As Date, cbo As ComboBox, num As Integer)
    2. '==================================================================
    3. Dim dtMonday As Date, iDay%, i%
    4.  
    5.     iDay = Weekday(dt, vbMonday)
    6.     If Not iDay = 1 Then
    7.         dtMonday = DateAdd("d", -(iDay - 1), dt)
    8.     Else
    9.         dtMonday = dt
    10.     End If
    11.    
    12.     For i = 0 To num
    13.         cbo.AddItem Format(DateAdd("d", i * 7, dtMonday), "mm/dd/yyyy")
    14.     Next i
    15.    
    16.     'find current week
    17.     iDay = Weekday(Date, vbMonday)
    18.     If Not iDay = 1 Then
    19.         dtMonday = DateAdd("d", -(iDay - 1), Date)
    20.     Else
    21.         dtMonday = Date
    22.     End If
    23.    
    24.     For i = 0 To cbo.ListCount - 1
    25.         If cbo.List(i) = Format(dtMonday, "mm/dd/yyyy") Then
    26.             cbo.ListIndex = i
    27.             Exit For
    28.         End If
    29.     Next i
    30.  
    31. 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
  •  



Click Here to Expand Forum to Full Width