Results 1 to 9 of 9
  1. #1
    John V is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jan 2014
    Posts
    43

    Populate combobox with all values between two values (inclusive)

    I am not sure that I am posting this in the correct forum, but here it goes.



    I am creating a database to track my income. The database includes tables for Employers, Venues and Events and Work Calls, and I have setup forms for entering the data into these tables.

    The Events Form allows me to enter the Event Name, Event Start Date, Event End Date, and Employer. I choose the Employer from a combobox that is populated from the Employers Table. These values are stored in the Events Table. The Employer is stored as a FK in the Events Table.

    On my Work Calls Form I would like to choose the event with a combobox populated from the Events Table and stored in the Events Table as a FK in the Work Calls Table (easy). I would then enter the Work Call Date from another combobox. What dates show up as choices in the Work Call Date combobox should depend on the Event chosen. I would like the dates in the list to include all dates inclusive of the Event Start Date and Event End Date.

    For example if I am working an event that has a Start Date of July 1,2014 and an End Date of July 5, 2014 I would like to have the combobox allow me to choose July 1, July 2, July 3, July 4 or July 5.

    I have searched and searched on the web and found nothing. Does anyone have any thoughts on how to accomplish this?

    Any help is appreciated.

    John V

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,626
    You have a table of all dates in the year for every year? If not, will need code to build the RowSource of the date combobox as a Value List or to populate a 'temp' table of dates.
    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
    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
    This will do the job; just replace cboDates, StartDate and EndDate with your actual names:

    Code:
    Private Sub cboDates_GotFocus()
     
     Dim Days As Integer
     Dim DayCount As Integer
     
     If Not IsNull(Me.StartDate) And Not IsNull(Me.EndDate) Then
     
      Me.cboDates.RowSource = ""
     
      DayCount = DateDiff("d", Me.StartDate, Me.EndDate)
     
      For Days = 0 To DayCount
       Me.cboDates.AddItem Format(DateAdd("d", Days, Me.StartDate), "mm/dd/yyyy")
      Next Days
     
     End If
    
    End Sub


    Linq ;0)>

  4. #4
    John V is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jan 2014
    Posts
    43
    No I don't have a table with all dates for every year. I believe Missinglinq is going in the direction I was looking for.

    John V

  5. #5
    John V is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jan 2014
    Posts
    43
    Thank you Missinglinq. I believe you are heading in the direction I am intending. I have a question regarding your code though. I will provide some more details about my database to assist you in answering my question.

    I have an Events Form (frmEvents) where I will enter the following information:

    Event Name (EventName) - This is a Text Box (txtEventName)
    Event Start Date (StartDate) - This is a Text Box (txtStartDate)
    Event End Date (EndDate) - This is a Text Box (txtEndDate)
    Employer Name (EmployerFK) - This is a ComboBox (cboEmployer)
    Venue Name (VenueFK) - This is ComboBox (cboVenue)

    This data is stored in an Events Table (tblEvents) under the fields in brackets.


    I will then have a Work Calls Form (frmCalls) for entering (among other things):

    Event Name (EventFK) - This is a ComboBox (cboEventName)
    Call Date (CallDate) - This is a Combo Box (cboDate)

    This data will be stored in a Calls Table (tblCalls) under the fields in brackets.


    The intention, for the Work Calls Form (frmCalls), is what dates appear in the Call Date ComboBox (cboDate) will be determined by what Event is chosen with cboEventName. As I mentioned in my previous post an event with a Start Date of July 1 and an End Date of July 5 should have July 1, July 2, July 3, July 4, and July 5 appear in the ComboBox (cboDate) list


    With all of this in mind my question is: Will the Me.StartDate and Me.EndDate in the DaysCount statement work since the StartDate and EndDate are not actually entered from the Work Calls Form, but rather from the Events Form (frmEvents) and stored in the Events Table (tblEvents)?

    Your help is greatly appreciated.
    John V

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,626
    Include the EventStartDate and EventEndDate fields as columns of the event combobox. Reference those columns in the code to construct the date combobox row source. Reference combobox columns by index. Index begins with 0.

    DayCount = DateDiff("d", Me.comboboxname.Column(x), Me.comboboxname.Column(y))

    Me.cboDates.AddItem Format(DateAdd("d", Days, Me.comboboxname.Column(x)), "mm/dd/yyyy")
    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.

  7. #7
    NTC is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    Nov 2009
    Posts
    2,392
    a combobox is fundamentally a query; and a query can have criteria that calls from text box controls of a form - in this case: >= me.StartDate AND <= me.EndDate

  8. #8
    John V is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jan 2014
    Posts
    43
    Thanks so much guys. Your suggestions solved my problem beautifully.

  9. #9
    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
    Glad we could help!

    Linq ;0)>

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

Similar Threads

  1. Replies: 8
    Last Post: 06-12-2014, 05:25 AM
  2. Combobox setting other combobox values problem
    By maxmaggot in forum Programming
    Replies: 8
    Last Post: 06-30-2013, 07:18 AM
  3. 1 Combobox with values from 2 tables
    By Comsoft in forum Access
    Replies: 1
    Last Post: 04-23-2013, 05:42 PM
  4. Populate Tables with Values from the Inernet
    By BallinWallin in forum Programming
    Replies: 2
    Last Post: 11-11-2011, 11:41 AM
  5. Values in a combobox
    By kbp in forum Access
    Replies: 2
    Last Post: 02-01-2011, 03:53 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