#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    30
    Rep Power
    1

    Question Change Text in TextBox based on selected item in combobox


    Ok, I have a combo box with three items: Mentor, Kirtland, and Euclid, and a textbox that displays the tax rate. The items each have respective tax rates of 3%, 2% and 5%. How do I set up a case statement so that depending on the item chosen, the correct tax rate is displayed?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    289
    Rep Power
    44
    Use 2 Arrays or a single 2 dimensional Array:
    Code:
    Option Explicit
    Dim strArray(2, 1) As String
    
    Private Sub Form_Load()
        strArray(0, 0) = "Mentor": strArray(0, 1) = "3%"
        strArray(1, 0) = "Kirtland": strArray(1, 1) = "2%"
        strArray(2, 0) = "Euclid": strArray(2, 1) = "5%"
        Combo1.Clear
        Combo1.AddItem strArray(0, 0)
        Combo1.AddItem strArray(1, 0)
        Combo1.AddItem strArray(2, 0)
    End Sub
    
    Private Sub Combo1_Click()
        Text1.Text = strArray(Combo1.ListIndex, 1)
    End Sub
    Just remember that if you want the list sorted, you have to sort the array first, and then load the list from the array. Using the Sorted Property on the Combo Box won't work properly.

    J.A. Coutts
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    30
    Rep Power
    1
    Originally Posted by couttsj
    Use 2 Arrays or a single 2 dimensional Array:
    Code:
    Option Explicit
    Dim strArray(2, 1) As String
    
    Private Sub Form_Load()
        strArray(0, 0) = "Mentor": strArray(0, 1) = "3%"
        strArray(1, 0) = "Kirtland": strArray(1, 1) = "2%"
        strArray(2, 0) = "Euclid": strArray(2, 1) = "5%"
        Combo1.Clear
        Combo1.AddItem strArray(0, 0)
        Combo1.AddItem strArray(1, 0)
        Combo1.AddItem strArray(2, 0)
    End Sub
    
    Private Sub Combo1_Click()
        Text1.Text = strArray(Combo1.ListIndex, 1)
    End Sub
    Just remember that if you want the list sorted, you have to sort the array first, and then load the list from the array. Using the Sorted Property on the Combo Box won't work properly.

    J.A. Coutts
    Nvm, got it working, you can have the mods delete this.

IMN logo majestic logo threadwatch logo seochat tools logo