Cycling through Combo Box ListItems

I have a combo box on a form. The form is opened up from another form passing in a parameter. The combo box contains that parameter amoung others in the list. How can I make the combo box select or goto that parameter when the form is opened? Example: say the parameter is "trees" so the 2nd form is opened up to the "trees" recordset. The combo box should have "trees" highlighted. Thanks.
Who is Participating?
BeedleGuisConnect With a Mentor Commented:
Dim inti As Integer
For inti = 0 To Combo1.ListCount
    Combo1.ListIndex = inti
    If UCase(Me.Combo1.Text) = ucase(ParamVal) Then
        Exit For
    End If
Next inti
use the code above in the procedure that recieves the parameter you are searching for.  If the form was previousley loaded, make sure to set combo1.listindex =0 before the for loop.
disregard that last need to set it to 0, because it does that in the loop...don't know what I was thinking.
Never miss a deadline with

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

BeedleGuis, you are correct but you may want to have it go

For inti = 0 to ComboBox.listcount - 1

as the index of the combobox starts from 0 meaning the last item is one less then the combobox's listcount, the same applies for anything else in an array
Try this

combo1 = "trees"
if combo1.listindex = -1 then ' not found
I myself wouldn't code it like that. The repeative changes to the ListIndex param will make it flicker.

Try something like this:

target = ucase(trim(target))
combo1.listindex = -1  'No item selected as default
for ndx = 0 to Combo1.ListCount -1
  if ucase(trim(Combo1.List(ndx))) = Target then
    combo1.ListIndex = ndx
    exit for
  end if
next ndx

This should be functionally equivalent but without the flicker as you go thru a (potentially) long list of items.


All Courses

From novice to tech pro — start learning today.