Setting the selected row in a Listbox

I have got to be making this harder than it is. So someone please tell me the straight forward way to do this:

I have a listbox with a list of people.
I have a textfield above the listbox that the user can type a few letters in to limit the displayed rows.

In other words, if all 492 people are currently displayed in the listbox and the user types a “p” in the textfield, then the listbox is reloaded with only the people whose last name begins with the letter p. When the user finds the person they are looking for they double click and the person’s details are populated to the window, the listbox reloaded with everyone, and the textfield cleared and set to “”

So far so good, this works perfectly. But I also want the person the user double-clicked on to be selected in the listbox (Highlighted) and the box scrolled to where that person is visible in the listbox control.

The brute-force method I am using is to read the rows in the listbox from top to bottom and compare the rowtag which contains the ID of the person selected. when they match select the row and scroll the listbox to that row.

this is the code in the doubleclick event:

dim iRow      As Integer
dim iRecID    As Integer
dim i         As Integer

iRow   = Me.ListIndex
iRecID = Me.RowTag(iRow)

if bFiltered Then
  me.selected(iRow) = true 
  tfLastNameSearch.Text = ""
  i = 0
  While i < me.ListCount
    if me.RowTag(i) = iRecID then 
      me.Selected(i) = true
      me.ScrollPosition = i - 1
    end if
    i = i + 1
end if

This code works correctly, BUT is there a more straightforward way to do this?

Why wouldn’t you select the item when adding it to the listbox instead of looping over the members again?

Because after the selected person is displayed, the listbox is reloaded with a different number of records and the row number will be different.

But you have the tag of the item you want selected. Why not check while you’re populating the list? Something like:

me.addrow newRowData Me.rowtag(me.lastindex) = newRowTag If newRowTag = iRecID then Me.listindex = I End if

That would avoid having to go through the list again.

Thanks Greg, I really was staring at the obvious and not seeing it.