ListView Item Select

  • Thread starter Thread starter Astidis
  • Start date Start date
A

Astidis

I currently have a listview item in a form that I periodically update due to
data changes. After the update, the ListView control defaults to the first item
added to the control. Is there a way in code to force the active pick to a
certain entry?

Thanks,

Alex
 
If you know the position of the item you want selected, you can do something
like:

Me!MyListBox.Selected(2) = True

(where MyListBox is the name of the list box, and you want the 3rd row
selected)

If you don't know the position, you'll have to loop through all of the rows
in the listbox until you find the one you want. If the listbox is bound to
the field of interest, you can use:

For intLoop = 0 To (Me!MyListBox.ListCount - 1)
If Me!MyListBox.ItemValue(intLoop) = "whatever" Then
Me!MyListBox.Selected(intLoop) = True
Exit For
End If
Next intLoop

If you're displaying a different field than what it's bound to, try

For intLoop = 0 To (Me!MyListBox.ListCount - 1)
If Me!MyListBox.Column(1, intLoop) = "whatever" Then
Me!MyListBox.Selected(intLoop) = True
Exit For
End If
Next intLoop
 
To test your suggestion, I created a control button on the form containing the
listview control and added the following control behind it:

Me!ListView.Selected(2) = True

where ListView is the name of the control. Upon clicking the button, I received
a Microsoft Visual Basic Dialog box reading:

Run-time error '438':
Object doesn't suport this property or method.

Did I do something wrong?
 
If this is a true Listview, you need something like this:

Me.NameOfYourListView.Object.Selected(2)=True
 
Hi,
You could try this:
Me!ListView.ListItems(2).Selected = True

A listview is made up of ListItems. A ListItem has a
Selected property, not the ListView itself, hence the error.
 
Dan,

That did it. Thanks for the help. I was even able to pass in a key
string and the listview found the row I requested.

Alex
 
Back
Top