How to get a Combo Box to feed from another Combo Box

Hope someone can help me!!

I have a form with two combo boxes. One displays different Areas(e.g. Area1, Area2, Area3) and the other one displays different Regions(e.g. Region1, Region2, Region3). What I want to do is to populate the Region Combo Box depending on the Area selected in the Area Combo Box. For Example if the user selects Area1 have the Region Combo Box filter/display on those Regions that belong to Area1.

I relly appreciate any feedback.

Thanks alot!!!



Answer this question

How to get a Combo Box to feed from another Combo Box

  • Niro

    country is declared as a string, it is set equal to the selected value of combobox1 when PopulateCities is called.

    So in your case country might be replaced with CarMake.   The Select allows your code to make an appropriate decision depending on what the user has selected.

    Your code would look something like this:

    Public Sub PopulateCarModels(ByVal CarMake As String)
         Me.ComboBox2.Items.Clear()
              
    Select Case CarMake
                   
    Case "Ford"
                     
    'Add all of your Ford models here
                   
    Case "Chevy"
                      
    'Add all of your Chevy models here
                    Case Else
                     
    'Add defult values or prompt the user to select a Make
                   
    End Select
    End Sub

    You can write a similare function and use it to set the text value of the price textbox like this:

    PriceTextBox.text = GetCarPrice(CarMake)

    Hope this helps

     

     



  • dbldown768

    The following is a simple example. In this I'm populating the items by simply adding the items to the 2nd combobox but you could populate from a database if you wanted to by modifying the code.

    Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Me.ComboBox1.Items.Clear()
    Me.ComboBox1.Items.Add("UK")
    Me.ComboBox1.Items.Add("USA")
    End Sub


    Sub PopulateCities(ByVal country As String)
    Me.ComboBox2.Items.Clear()
    Select Case country
    Case "UK"
    Me.ComboBox2.Items.Add("London")
    Me.ComboBox2.Items.Add("Glasgow")
    Me.ComboBox2.Items.Add("Bristol")
    Case "USA"
    Me.ComboBox2.Items.Add("New York")
    Me.ComboBox2.Items.Add("Philadelphia")
    Me.ComboBox2.Items.Add("Orlando")
    Case Else
    End Select
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
    Me.PopulateCities(Me.ComboBox1.Text)
    Me.ComboBox2.Text = ""
    End Sub
    End Class


  • Tryin2Bgood

    I have a similar situation. I'm user a combo box for car make and a combo box for car model. I want the models to show up that are appropriate with the car make. This thread is helpful but what does the "Select case country" mean. I'm trying almost the same thing but how is "country" defined as a variable.

    Also, when a user selects the car make from the combo box, I want a price to automatically show in a text box. Any ideas on how this would work. I'd appreciate it. Thanks


  • Andreas Hallberg

    Thanks alot. It worked

    I really appreciate the quick response!!!


  • spinal

    How would you modify this code to populate these boxes from a database


  • bberti

    Well thats fine, you can call the method from anywhere within your application/class. The method will get whatever text is selected in comboBox1 and pass that to the PopulateCities method for it to add to the specified comboBox

    in this case when you make a selection from the comboBox, the text is chosen but also the selectedIndexChanged event gets fired automatically, since you are changing your selection from the combobox, so once it fires, it sends/calls the method giving it the new item that has been selected



  • Sandro Haupt

    Thats makes more sense thanks. But i'm still a bit confused on another thing. What is populatecarmodels is that a button or is that the first combo box
  • Tom2U

    im afraid these forums are ONLY for .NET Development, not for VBA.



  • Chris Baldwin - MSFT

    PopulateCarModels is a subroutine. Just for your information, a subroutine does not return a value, where as a function returns a value to the caller in most cases. The Public declaration means that it can be accsessed from outside of the form code, e.g. another form, if you did not want it avilable outside of the form you would declare it as Private. Thus the declaration is:

    Public Sub PopulateCarModels(ByVal CarMake As String)

    To call it you would just write

    PopulateCarModels(Combobox1.text)

    Hope this helps



  • ReneeC

    I'm a noob, so bare with me. What happens if you decide not to call a selectedindexchanged event for comboBox1 What if you just called the method and entered the comboBox1.text to it, like so:

    ' Call the function

    PopulateCities(ComboBox1.Text)



  • Senthil A

    Hello.I'm also a beginer in visual basic development and I also have problems withs this code(i use this code in access database ).It seems to work fine until I go back in the first combo box on the saim form.For my code look like this :

    "Private Sub Form_Load()

    Me.CboMarca.AddItem "Audi"
    Me.CboMarca.AddItem "Bmw"
    Me.CboMarca.AddItem "Mercedes-Benz"
    Me.CboMarca.AddItem "Opel"
    Me.CboMarca.AddItem "Seat"
    Me.CboMarca.AddItem "Skoda"
    Me.CboMarca.AddItem "Volswagen"

    End Sub

    Private Sub Cbomodel_Enter()

    Select Case Carmodel

    Case "Opel"

    me.cbomodel.additem "Astra"

    me.cbomodel.additem"Vectra"

    etc .....

    Case "Bmw"

    me.cbomodel.additem "Seria 3"

    etc ...

    case else

    End select "

    If I came back to the first combo box and change the item from "opel " to "Bmw" the second combo box shows a list witch its containing the list from opel and bmw .

    My questions is : is there any way that I can avoid this or is there another procedure to do this .

    Thx



  • How to get a Combo Box to feed from another Combo Box