Results 1 to 5 of 5
  1. #1
    jle0003 is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Apr 2012
    Posts
    60

    Adding values to a combobox with code

    Can someone provide a code sample to add values to a combobox please? Do you use the AddItems property?

    Thanks!

  2. #2
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows XP Access 2003
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,530
    Quote Originally Posted by jle0003 View Post
    Can someone provide a code sample to add values to a combobox please? Do you use the AddItems property?

    Thanks!
    A combo box can be populated in a number of ways which will need different methods to add to it. Please show us the "Row Source Type" property setting and the "Row Source" property setting.
    Can you tell us a bit more about the data in the combo box and what you want to do.
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  3. #3
    jle0003 is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Apr 2012
    Posts
    60
    I posted my issue a day or 2 ago and got a reply with a possible solution but I am inexperienced and on a tight timeline so I was hoping for something a little quicker and less complicated. Here is the link to the thread: https://www.accessforums.net/showthr...340#post120340

    I've decided to use only 2 comboboxes and have the second dependent on the selection from the first. Each field is in a separate table though.

    I was hoping that I could use code to do something like what I have below where the combobox is cleared out each time the form is opened and populated with values based on what the user selects in the first combobox...

    If cboOne = "A" Then
    cboTwo options are [1,2,3]
    Elseif cboOne ="B" Then
    cboTwo options ae [10,20,30]
    Elseif cboOne = "C" Then
    cboTwo options are [100,200,300]
    End if

    The Row Source is blank/empty and the Row Source Type is set to "Value List" for cboTwo.

  4. #4
    ssanfu is offline Master of Nothing
    Windows 2K Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    What you are looking for is "Cascading combo boxes". Try Googling that......

  5. #5
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows XP Access 2003
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,530
    Maybe this is what you are trying to do:

    Create a new form with two combo boxes called “ComBox1” and ComBox2”.
    Code:
    Private Sub ComBox1_AfterUpdate()
      'Reset the value of ComBox2
      Me.ComBox2 = ""
      Me.ComBox2.SetFocus
    End Sub
     
    Private Sub ComBox2_GotFocus()
    'Find out the value of ComBox1 and then
    'Set the list values of ComBox2
    Select Case Me.ComBox1
      Case Is = "fruit"
        Me.ComBox2.RowSourceType = "Value List"
        Me.ComBox2.RowSource = "Apple;Orange;Grape"
      Case Is = "Veg"
        Me.ComBox2.RowSourceType = "Value List"
        Me.ComBox2.RowSource = "Pea;Turnip;Grape"
      Case Else
      
    End Select
    End Sub
     
    Private Sub Form_Current()
      'Empty and setup ComBox2
      Me.ComBox2.RowSourceType = "Value List"
      Me.ComBox2.RowSource = ""
      'Empty and setup ComBox1
      Me.ComBox1.RowSourceType = "Value List"
      Me.ComBox1.RowSource = "Fruit;Veg"
      Me.ComBox1 = ""
    End Sub 


    Set the form’s On Current property to [Event Procedure]
    Set the After Update event of ComBox1 to [Event Procedure]
    Set the Got Focus event of ComBox2 to [Event Procedure]

    See attached db

    IMHO this is not the best way to handle this (that has already been given to you), but it should work.
    Attached Files Attached Files
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

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

Similar Threads

  1. ComboBox not listing values
    By tylerg11 in forum Access
    Replies: 1
    Last Post: 09-23-2011, 10:28 AM
  2. vba code not working with combobox
    By bopsgtir in forum Programming
    Replies: 9
    Last Post: 02-24-2011, 11:27 AM
  3. Values in a combobox
    By kbp in forum Access
    Replies: 2
    Last Post: 02-01-2011, 03:53 AM
  4. Replies: 4
    Last Post: 12-03-2010, 04:05 PM
  5. Code in combobox, code in text box
    By float in forum Forms
    Replies: 3
    Last Post: 09-29-2010, 07:12 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