ListBox Question

B

Bret

This discussion group provided the code below that works wonderfully.
It takes a number from a text box that user has entered, searches for that
number in the first column of the listbox and then Selects it.
The problem is the Listbox has about 300 records so, if for example the
found record is the 200th record, user has to use the Vertical Scroll bar to
see the selected record. I've tried .requery, .repaint, .setFocus and haven't
found a way for the listbox to scrolldown to the selected records so user
doesn't have to.

Please can this be done programatically.
thanks.

With Me.Listbox
If .ListCount > 0 Then
'.Selected(0) = True
For i = 0 To .ListCount - 1
If .Column(0, i) Like strSearchNumber Then
.Selected(i) = True
End If
Next i
End If
End With
 
M

Marshall Barton

Bret said:
This discussion group provided the code below that works wonderfully.
It takes a number from a text box that user has entered, searches for that
number in the first column of the listbox and then Selects it.
The problem is the Listbox has about 300 records so, if for example the
found record is the 200th record, user has to use the Vertical Scroll bar to
see the selected record. I've tried .requery, .repaint, .setFocus and haven't
found a way for the listbox to scrolldown to the selected records so user
doesn't have to.

With Me.Listbox
If .ListCount > 0 Then
'.Selected(0) = True
For i = 0 To .ListCount - 1
If .Column(0, i) Like strSearchNumber Then
.Selected(i) = True
End If
Next i
End If
End With


If the list box's MultiSelect property is set to None, then
you can use the ListIndex property
.SetFocus
.ListIndex = i

OTOH, if the list box's BoundColumn is set to one, then you
do not need any of that stuff. Just set the list box's
value to the search number.

If the list box's MultiSelect is set to Simple, the list
index idea will probably work. For Extended, I think it
will deselect all the other selections.
 
B

Bret

Marsh,
Your second option of searching by me.listbox.value if the listbox
MultiSelect is set to NONE works greate. Unfortunately I have other
functionalities in place on this form that required the MultiSelect to be
"SIMPLE" and then the codes dont work.

So what I did as a workaround in code is this.
1) Set me.mylistbox.multiSelect = 0 for "None"
2) Run your code
3) Set me.mylistbox.multiSelect = 1 for "Simple"

I took the syntax of the multiSelect straight from the EXAMPLE in help box
however I get the error message that "can't assign a value to this object".
What is wrong with the MultiSelect coding?

thanks again for any help
 
M

Marshall Barton

I think you forgot about using the selected property and are
making it too complicated. This worked in my test:

.SetFocus
.ListIndex = i
Me.List0.Selected(i) = True
 
B

Bret

Wonnerful! That works, yes I was missing that line of code.
Much much thanks!

Marshall Barton said:
I think you forgot about using the selected property and are
making it too complicated. This worked in my test:

.SetFocus
.ListIndex = i
Me.List0.Selected(i) = True
--
Marsh
MVP [MS Access]

Your second option of searching by me.listbox.value if the listbox
MultiSelect is set to NONE works greate. Unfortunately I have other
functionalities in place on this form that required the MultiSelect to be
"SIMPLE" and then the codes dont work.

So what I did as a workaround in code is this.
1) Set me.mylistbox.multiSelect = 0 for "None"
2) Run your code
3) Set me.mylistbox.multiSelect = 1 for "Simple"

I took the syntax of the multiSelect straight from the EXAMPLE in help box
however I get the error message that "can't assign a value to this object".
What is wrong with the MultiSelect coding?
 

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