Moving to an entry in a listbox

  • Thread starter JimBurke via AccessMonster.com
  • Start date
J

JimBurke via AccessMonster.com

I have a multi-select listbox that is sorted in name order. Is there a way to
get the listbox to act like a combobox, where you can start typing the entry
you want and it will 'scroll' to that section of the list as you keep typing?
Mine will only accept one letter - i.e. if I want to get to Smith, I type in
"S" and it goes to the first name starting with "S". But as soon as I type in
an "M" it moves to the first "M' name in the list, rather than moving to the
first name starting with "SM". This technique works in my comboboxes, but not
in listboxes. There are hundreds of entries in the listbox, so it's a bit of
a hassle for the users to only be able to go to the first entry for that
letter and then have to scroll through.
 
F

fredg

I have a multi-select listbox that is sorted in name order. Is there a way to
get the listbox to act like a combobox, where you can start typing the entry
you want and it will 'scroll' to that section of the list as you keep typing?
Mine will only accept one letter - i.e. if I want to get to Smith, I type in
"S" and it goes to the first name starting with "S". But as soon as I type in
an "M" it moves to the first "M' name in the list, rather than moving to the
first name starting with "SM". This technique works in my comboboxes, but not
in listboxes. There are hundreds of entries in the listbox, so it's a bit of
a hassle for the users to only be able to go to the first entry for that
letter and then have to scroll through.

Sorry, a List box does not work the same as a Combo box.

If you enter just the first letter "S" repeatedly, it will bring each
value up that begins with "S". However if you sort the column (in the
List Box Rowsource) then after pressing the first "S" all of the S
records will appear, in order, i.e. Sally, Sam, Selma, Steve, etc.
When you are at the first "S" value all of the additional "S" rows are
visible just below it. Use the List Box scroll bar if you have to go
down many rows.

Perhaps with hundreds of rows, a combo might be more suitable anyway.
 
J

Jack Leach

I've never tried it before, but if you would be willing to settle for having
an unbound textbox to search the list (rather than typing in the list
itself), you may be able to force the currently selected item to the list
item returned by some sort of custom findfirst function (search the names,
first one Like "abc*"). It may take a little coding to do this, but I
wouldn't think it impossible.

Being a multi select you might have to gather a list of selected items and
hopefully somehow discern the currently highlighted but not selected(?) items
to retain them...

This is purely off the top of my head (I've never used more than a handful
of list boxes myself), but it may be worth looking into.

hth
--
Jack Leach
www.tristatemachine.com

"I haven't failed, I've found ten thousand ways that don't work."
-Thomas Edison (1847-1931)
 
J

JimBurke via AccessMonster.com

I would if I could - they need to be able to select multiple values, often a
large number of them.
I have a multi-select listbox that is sorted in name order. Is there a way to
get the listbox to act like a combobox, where you can start typing the entry
[quoted text clipped - 6 lines]
a hassle for the users to only be able to go to the first entry for that
letter and then have to scroll through.

Sorry, a List box does not work the same as a Combo box.

If you enter just the first letter "S" repeatedly, it will bring each
value up that begins with "S". However if you sort the column (in the
List Box Rowsource) then after pressing the first "S" all of the S
records will appear, in order, i.e. Sally, Sam, Selma, Steve, etc.
When you are at the first "S" value all of the additional "S" rows are
visible just below it. Use the List Box scroll bar if you have to go
down many rows.

Perhaps with hundreds of rows, a combo might be more suitable anyway.
 
J

JimBurke via AccessMonster.com

Sounds like a whole little application in and of itself! Could be interesting
to try, but... I guess I could change it so that they use a combobox and
select them one at a time, with each selection going to a list of selected
names. If they aren't selecting many adjacent names then I guess that might
be easier for the users.

Jack said:
I've never tried it before, but if you would be willing to settle for having
an unbound textbox to search the list (rather than typing in the list
itself), you may be able to force the currently selected item to the list
item returned by some sort of custom findfirst function (search the names,
first one Like "abc*"). It may take a little coding to do this, but I
wouldn't think it impossible.

Being a multi select you might have to gather a list of selected items and
hopefully somehow discern the currently highlighted but not selected(?) items
to retain them...

This is purely off the top of my head (I've never used more than a handful
of list boxes myself), but it may be worth looking into.

hth
I have a multi-select listbox that is sorted in name order. Is there a way to
get the listbox to act like a combobox, where you can start typing the entry
[quoted text clipped - 6 lines]
a hassle for the users to only be able to go to the first entry for that
letter and then have to scroll through.
 
J

John Spencer MVP

There is a solution for a listbox that does not use multi-select.

It uses a text control to enter values and then that select the specific
record in the listbox.

Given a textbox named txtLastName and a listbox named lstResults, use the
following code for the change event of txtLastName. As you type into the text
box then listbox selection will change.

An interesting challenge.

By the way the solution is based on Arvin Meyer's Expanding Listbox Autofill
example available at his site
http://www.datastrat.com/Download2.html
Look for ExpandingSearch## and download the appropriate version.

Private Sub txtLastName_Change()
Dim SearchValue As Variant 'What are we looking for
Dim iCount As Integer 'Loop Counter
Dim iStart As Integer 'Start Loop at first data row
Dim iSearchColumn As Integer 'Specify column to search
Dim iMatch As Integer 'Row number that matches

iSearchColumn = 1
iStart = Abs(Me.lstResults.ColumnHeads)
iMatch = -1

SearchValue = Me.txtLastName.Text


For iCount = iStart To lstResults.ListCount - 1
If InStr(1, lstResults.Column(iSearchColumn, iCount) _
, SearchValue, 1) = 1 Then
iMatch = iCount
Exit For
End If
Next iCount

If iMatch = -1 Then
Beep
Me.lstResults = Null
Else
Me.lstResults = Me.lstResults.ItemData(iCount)
End If

End Sub

If you want to use this with a multi-select listbox, you will need to modify
(replace) the line
Me.lstResults = Me.lstResults.ItemData(iCount)
and also have something that tells you when the user has finished selecting an
item so you don't get multiple items selected as the user types.

Changing that one line to
' Me.lstResults = Me.lstResults.ItemData(iCount)
Me.lstResults.SetFocus
Me.lstResults.ListIndex = iCount - 1
Me.txtLastName.SetFocus
Me.txtLastName.SelStart = 255
allows you to work with multi-select lists; however it clears items selected list.

This looks like a challenge I might investigate.
John Spencer
Access MVP 2002-2005, 2007-2009
The Hilltop Institute
University of Maryland Baltimore County
 
F

fredg

Sounds like a whole little application in and of itself! Could be interesting
to try, but... I guess I could change it so that they use a combobox and
select them one at a time, with each selection going to a list of selected
names. If they aren't selecting many adjacent names then I guess that might
be easier for the users.

Jack said:
I've never tried it before, but if you would be willing to settle for having
an unbound textbox to search the list (rather than typing in the list
itself), you may be able to force the currently selected item to the list
item returned by some sort of custom findfirst function (search the names,
first one Like "abc*"). It may take a little coding to do this, but I
wouldn't think it impossible.

Being a multi select you might have to gather a list of selected items and
hopefully somehow discern the currently highlighted but not selected(?) items
to retain them...

This is purely off the top of my head (I've never used more than a handful
of list boxes myself), but it may be worth looking into.

hth
I have a multi-select listbox that is sorted in name order. Is there a way to
get the listbox to act like a combobox, where you can start typing the entry
[quoted text clipped - 6 lines]
a hassle for the users to only be able to go to the first entry for that
letter and then have to scroll through.

You could have an unbound control on the form.
Then using the AfterUpdate event of a combo box, code:
Me.[ControlName] = Me.[ControlName] & Me.[ComboName] & ","

Then use [ComboName] where you would have used the Multi Select List
box value.
Remember to use code to remove the trailing comma before you use the
concatenated value.
 
J

John Spencer MVP

Actually when I tested further I found my code did NOT clear any selection
made in a multi-select list. It moves the listbox to the item as you type,
but the user would still have to click on the item in the listbox to add it to
the list's selected items.

That may be best anyway, since I doubt you would want to type SPE (for
example) in the text control and have it automatically select the first item
that began with S, then the first item that began with SP, and then the first
item that began with SPE. You would probably only want the item that began
with SPE to be selected.

John Spencer
Access MVP 2002-2005, 2007-2009
The Hilltop Institute
University of Maryland Baltimore County
There is a solution for a listbox that does not use multi-select.

It uses a text control to enter values and then that select the specific
record in the listbox.
SNIP
 
J

JimBurke via AccessMonster.com

That's exactly what I would want - they should still have to select the item.
I had given some thought to coding something along those lines, but thought
it might take too much time. I might have to try this method - I was starting
to go with a combobox being used to select each name and add them to a list,
but to do it all via a listbox is ideal. Thanks for the code, I appreciate it.
 
J

JimBurke via AccessMonster.com

I need to have a simple way of knowing exactly which ones were selected (this
method would involve parsing I would think), and also an easy way to de-
select an item if needed.
Sounds like a whole little application in and of itself! Could be interesting
to try, but... I guess I could change it so that they use a combobox and
[quoted text clipped - 22 lines]
You could have an unbound control on the form.
Then using the AfterUpdate event of a combo box, code:
Me.[ControlName] = Me.[ControlName] & Me.[ComboName] & ","

Then use [ComboName] where you would have used the Multi Select List
box value.
Remember to use code to remove the trailing comma before you use the
concatenated value.
 

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