Listbox - Keeping selected item in view

  • Thread starter Thread starter Reid Kell
  • Start date Start date
R

Reid Kell

I have a search function for multi-select Listbox that works just fine -- in
a textbox, the user enters an ID and I then select it in the Listbox.
However, the Listbox contains about 200 items and the selection is often way
out of the immediate view. How do I programatically scroll down in the
Listbox so the selected item appears? I'm using Access 2000. Thanks.
 
-----Original Message-----
I have a search function for multi-select Listbox that works just fine -- in
a textbox, the user enters an ID and I then select it in the Listbox.
However, the Listbox contains about 200 items and the selection is often way
out of the immediate view. How do I programatically scroll down in the
Listbox so the selected item appears? I'm using Access 2000. Thanks.


.
Hi Reid,
how are you selecting the item in the listbox?
Luck
Jonathan
 
For x = 0 To ctl.ListCount - 1
If ctl.ItemData(x) = lngSR Then
ctl.Selected(x) = True
End If
Next x
 
Hi Reid,
if only 1 item in the list can match lngSR then set
multiselect to none and use

ctl.value=lngSR

I haven't figured out solution for multiselect list to
scroll to first selected item

Luck
Jonathan
 
See:
http://www.lebans.com/List_Combo.htm#ScrollListbox
Scroll a ListBox to a specific row. Emulates the VB ListBox TopIndex
property. You can alter the code to easily have the selected row display
as the first or last row as well. The example code is placed behind a
Command Button.

' *** CODE START
Private Sub cmdListIndex_Click()
On Error GoTo Err_cmdListIndex_Click

' Always make NumRows an odd number
' if you want selected Row to be in the
' middle of the ListBox.

' NumRows is the number of completely visible rows in the ListBox Const
NumRows = 7
' Row we want displayed in middle of ListBox.
Dim intDesiredRow As Integer

' Arbitrarily select the 24th row.
intDesiredRow = 24
' ListBox must have the Focus
Me.List2.SetFocus
' Force ListBox to start from the top
Me.List2.ListIndex = 1

' Force the Scroll offset we desire
Me.List2.ListIndex = intDesiredRow + (NumRows / 2)
' Now select the row without further scrolling
Me.List2.ListIndex = intDesiredRow

Exit_cmdListIndex_Click:
Exit Sub

Err_cmdListIndex_Click:
MsgBox Err.Description
Resume Exit_cmdListIndex_Click

End Sub
' ***CODE END



--

HTH
Stephen Lebans
http://www.lebans.com
Access Code, Tips and Tricks
Please respond only to the newsgroups so everyone can benefit.
 
Sweet! This worked great. Note that errors occur when searching for the last
item in the list because of the attempt to offset. Thus, I simply skipped
that part to avoid hassles. Thanks a million.
 
Back
Top