Results 1 to 8 of 8
  1. #1
    crowegreg is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Posts
    398

    Listbox populated with dates

    I need a listbox populated with the calandar days of the month, an example would be for December have the selections of 12/01/2014, 12/02/2014, etc.

    Is this possible?

    Thanks in adance for your assistance!!

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,772
    The listbox RowSource can be built with code.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    crowegreg is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Posts
    398
    Can you get me started on the code?

    As an example, we're in December, so I need 12/25/2014, 12/26/2014, 12/27/2014, etc.

    In January I need 01/19/2015, 01/20/2015, 01/21/2015, etc.

    Thanks!!

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,772
    The combobox RowSourceType property must be set to ValueList.

    Private Sub List101_GotFocus()
    Dim i As Integer
    For i = 1 To 31
    Me.List101.AddItem "Jan " & i & " 2014"
    Next
    End Sub

    Making the upper limit of the For loop and the month and year all dynamic is the tricky part. Start with this static example. When that works, figure out how to make it dynamic (user input or current date).
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    crowegreg is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Posts
    398
    Thanks!! I couldn't come up with the code.

    Happy Holidays!!

  6. #6
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Quote Originally Posted by June7 View Post

    ...Making the upper limit of the For loop and the month and year all dynamic is the tricky part...
    Here's some code I wrote for someone a while ago that takes care of the tricky part:

    As June7 said, go to Properties - Data, for the Listbox, and set the RowSource Type to Value List, then use this code, replacing lstDates, in the code, with the actual name of your Listbox:
    Code:
    Private Sub Form_Load()
     
     Dim Days As Integer
     Dim DayCount As Integer
     Dim StartDate As Date
     Dim EndDate As Date
    
     StartDate = CDate(DateSerial(Year(Date), Month(Date), 1))
     EndDate = CDate(DateSerial(Year(Date), Month(Date) + 1, 0))
    
     Me.lstDates.RowSource = ""
    
     DayCount = DateDiff("d", StartDate, EndDate)
    
      For Days = 0 To DayCount
       Me.lstDates.AddItem Format(DateAdd("d", Days, StartDate), "mm/dd/yyyy")
      Next Days
    
    End Sub

    I have to say I'd use a Combobox for this, instead of a Listbox, simply because of the space involved, but the code for either, in this case, would be the same. When using the Combobox, I'd probably use the GotFocus event, as June7 did, but an empty Listbox, when the Form loads, looks sort of dorky, which is why I used the Form_Load event; the code will work in either.

    Linq ;0)>

  7. #7
    kemas is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Jul 2011
    Posts
    6
    Private Sub List101_GotFocus()
    Dim i As Integer
    For i = 1 To 31
    Me.List101.AddItem Year(Date) & "/" & Month(Date) & "/" & i
    Next
    End Sub

  8. #8
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,772
    Not every month has 31 days. Linq's code deals with that.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 2
    Last Post: 03-23-2014, 06:50 AM
  2. Replies: 4
    Last Post: 06-24-2013, 07:34 AM
  3. Replies: 3
    Last Post: 12-13-2012, 04:40 AM
  4. Replies: 1
    Last Post: 09-10-2012, 11:21 PM
  5. Replies: 1
    Last Post: 07-26-2012, 11:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums