Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamically removing items from combo box

Status
Not open for further replies.

cm80

Technical User
May 3, 2001
85
0
0
US
Hi,
If you could please have a look at the code below here is the problem I am having.
If I select Accommodation the combo box is populated fine, however if I go ahead and select Restaurants from the original combo box the accommodation values are still there.

How can i make it so that if the user changes their selection in cboSelCat, previous entries in the combo box cboSelSubCat will be removed?

Thanks



Private Sub cboSelCat_Click()
Select Case cboSelCat.Text
Case "Accommodation"
cboSelSubCat.AddItem "Hotels"
cboSelSubCat.AddItem "Guesthouses"
cboSelSubCat.AddItem "Hostels"
Case "Restaurants"
cboSelSubCat.AddItem "Chinese"
cboSelSubCat.AddItem "Italian"
cboSelSubCat.AddItem "Mexican"
cboSelSubCat.AddItem "Other"
Case Else
cboSelSubCat.AddItem "Empty"
End Select
End Sub
 
Private Sub cboSelCat_Click()
Select Case cboSelCat.Text
Case "Accommodation"
cboSelSubCat.list(0)= "Hotels"
cboSelSubCat.list(1)= "Guesthouses"
cboSelSubCat.list(2)= "Hostels"
Case "Restaurants"
cboSelSubCat.list(0)= "Chinese"
cboSelSubCat.list(1)= "Italian"
cboSelSubCat.list(2)= "Mexican"
cboSelSubCat.list(3)= "Other"
Case Else
cboSelSubCat.clear
End Select
End Sub
 
ChiuChimu,
That code will still leave "Other" cboSelSubCat.list(3) left over from "Restaurants". You need the Clear method to get rid of cboSelSubCat.list(3)
Private Sub cboSelCat_Click()
Select Case cboSelCat.Text
cboSelSubCat.clear
Case "Accommodation"
cboSelSubCat.list(0)= "Hotels"
cboSelSubCat.list(1)= "Guesthouses"
cboSelSubCat.list(2)= "Hostels"
Case "Restaurants"
cboSelSubCat.list(0)= "Chinese"
cboSelSubCat.list(1)= "Italian"
cboSelSubCat.list(2)= "Mexican"
cboSelSubCat.list(3)= "Other"
End Select
End Sub
Compare Code (Text)
Generate Sort in VB or VBScript
 
Oops. One had 3 and the other had 4! My point was to get the string in the list without using additem. I leave the finer details to you - Gentlemen of the Forum.
 
your code should be

Private Sub cboSelCat_Click()

cboSelSubCat.clear
Select Case cboSelCat.Text
Case "Accommodation"
cboSelSubCat.AddItem "Hotels"
cboSelSubCat.AddItem "Guesthouses"
cboSelSubCat.AddItem "Hostels"
Case "Restaurants"
cboSelSubCat.AddItem "Chinese"
cboSelSubCat.AddItem "Italian"
cboSelSubCat.AddItem "Mexican"
cboSelSubCat.AddItem "Other"
Case Else
cboSelSubCat.AddItem "Empty"
End Select
End Sub


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top