Link to home
Start Free TrialLog in
Avatar of Sandra Smith
Sandra SmithFlag for United States of America

asked on

Remove items from a combo in code

I have a combo box that its contents depends on what a user selects in the first combo.  These are boxes located on a data entry fform bound to a table.  So, depending on what the user selects in the first combo box, the second combo needs to have its data dynamically change.  I tried the below, but I keep getting a Compile Error User Defined Type Note defined.  All I want is to clear the (*)*I^&%% combo box.  I thought it woudl be so simple!
Public Sub ClearSecondary(ctl As Combo)
'Clears Secondary combo boxes so there is no left over item
Dim i As Integer
           For i = 1 To ctl.Count
               ctl.RemoveItem 0
           Next i
End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of aesmike
aesmike

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Sandra Smith

ASKER

Neither, it is a list I have in a public sub that, depending on the first combo, is call to populate the second combo.  Actually, i got the code straight from microsoft's examples.  I changed it to below, but now the count is giving me problems.
Public Sub ClearSecondary()
'Clears Secondary combo boxes so there is no left over item
Dim i As Integer, ctl As ComboBox
Set ctl = Forms!frmWeeklyActivities.cboSecondary
           For i = 1 To ctl.Count
               'Remove an item from the ListBox.
               ctl.RemoveItem 0
           Next i
End Sub

Open in new window

HA! I got it to work.  I changed it to the following.
Public Sub ClearSecondary()
'Clears Secondary combo boxes so there is no left over item
Dim i As Integer, ctl As ComboBox
Set ctl = Forms!frmWeeklyActivities.cboSecondary
           For i = 1 To ctl.ListCount
               'Remove an item from the ListBox.
               ctl.RemoveItem 0
           Next i
End Sub

Open in new window

You were kind enought to responsd, thank you.
Avatar of aesmike
aesmike

Ohhh, you're doing it THAT way.
don't ya love how Access handles combo boxes? <g>
That way was such a pain, I just wrote functions built a value list string and set it to that.
Anyway, same thing applies.  Set the rowsource to a blank string.  (then set it back to the function name later)

try just setting the rowsource to a blank string then back to it's previous value.  two lines of code.