Listbox - Keeping selected item in view

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.
 
J

Jonathan Parminter

-----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
 
R

Reid Kell

For x = 0 To ctl.ListCount - 1
If ctl.ItemData(x) = lngSR Then
ctl.Selected(x) = True
End If
Next x
 
J

Jonathan Parminter

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
 
S

Stephen Lebans

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top