By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,871 Members | 2,575 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,871 IT Pros & Developers. It's quick & easy.

Using Listboxes to enable multi-select

P: n/a


Hi. I have two questions ...

(1) I want to use a Listbox to enable the user to select 1 or many items
from the list. However, I'm having trouble figuring out how to find out
t which items have been selected. How can I do that? Given the
listindex, .selected, .itemsselected, .itemdata properties -- I'm
getting confused.

(2) I, actually, have two listboxes on the form and I want to ONLY let
the user select items in one list. The selected items will be used to
create a query and the listboxes are mutually exclusive. In which event
routines should I put logic to either warn the user or disable the other
control. Please keep in mind that I want to make it such that they can
go back and forth between listboxes before they make up their mind and
hit the command button which will fire off the logic to build the query
and do the other stuff that I want done.

THANKS much for your help.

Regards,
SueB

*** Sent via Developersdex http://www.developersdex.com ***
Nov 13 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Susan Bricker wrote:

Hi. I have two questions ...

(1) I want to use a Listbox to enable the user to select 1 or many items
from the list. However, I'm having trouble figuring out how to find out
t which items have been selected. How can I do that? Given the
listindex, .selected, .itemsselected, .itemdata properties -- I'm
getting confused.
Dim varItem As Variant
Dim varValue As Variant
For Each varItem In Me.ListBox.ItemsSelected
'this gets the value of the first column
varValue = Me.ListBox.ItemData(varItem)
Next varItem

For Each varItem In Me.ListBox.ItemsSelected
'in case you need the value in another col besides the first
'remember, column starts at 0, not 1. So 1 is col 2
varValue = Me.ListBox.ItemData(varItem).Column(1)
Next varItem

(2) I, actually, have two listboxes on the form and I want to ONLY let
the user select items in one list. The selected items will be used to
create a query and the listboxes are mutually exclusive. In which event
routines should I put logic to either warn the user or disable the other
control. Please keep in mind that I want to make it such that they can
go back and forth between listboxes before they make up their mind and
hit the command button which will fire off the logic to build the query
and do the other stuff that I want done.
I suppose you could have a frame with 2 checkboxes; Use List1, Use
List2. From there you could enable/disable the list boxes and the
command button would know which listbox to use based on the frame value.

THANKS much for your help.

Regards,
SueB

*** Sent via Developersdex http://www.developersdex.com ***

Nov 13 '05 #2

P: n/a
Thanks for the quick response. Your examples of getting the selected
items from the listbox was just what I needed. As for the checkboxes,
good idea. I was trying to avoid another key stroke for the user, but
looks like your idea would be a good plan. Actually, I might look into
using an OptionGroup, since the user will only be able to select from 1
listbox or the other listbox. So, if I put some option buttons in an
optiongroup, if they select option 1 then that automatically will
"unselect" option 2 (and then I'll disable the associated listbox ...
forcing them to only be able to select from one listbox at a time).

Thanks for your ideas.

Regards,
SueB

*** Sent via Developersdex http://www.developersdex.com ***
Nov 13 '05 #3

P: n/a
Susan Bricker wrote:
Thanks for the quick response. Your examples of getting the selected
items from the listbox was just what I needed.
You are welcome.

As for the checkboxes, good idea. I was trying to avoid another key stroke for the user, but
looks like your idea would be a good plan. Actually, I might look into
using an OptionGroup, since the user will only be able to select from 1
listbox or the other listbox. So, if I put some option buttons in an
optiongroup, if they select option 1 then that automatically will
"unselect" option 2 (and then I'll disable the associated listbox ...
forcing them to only be able to select from one listbox at a time).
Sorry for the confusion. A frame is what I call an option group...since
the name of the option grroup usually begins with Frame. Yeah, I know
what you mean...adding keystrokes...always a pita. I felt that by using
this method your program would know which list box to use and you could
also enable/disable the listbox based on the selection. Sometimes
tradeoffs are required....

Thanks for your ideas.

Regards,
SueB

*** Sent via Developersdex http://www.developersdex.com ***

Nov 13 '05 #4

P: n/a
Hey, Salad...

Is there a way to "unselect" items in a listbox, programmatically?
Here's what I want to (and I've been trying to to this while debugging
the form, but I've been unsuccessful) ...

I have the option buttons in an options group: 1 button for the first
listbox and 1 button for the otgher listbox.

Let's say the user selects optionbutton 1 and enables the first listbox.
Then they select a few items in the first listbox.

Then they decide (before hitting the command button to generate the
query) that they really want to use the 2nd list box. So, they hit the
other option button, which will disable listbox 1 and enable listbox 2.
However, the items that were selected in listbox 1 are still
highlighted. I want to get those highlighted items unhighlighted
(unselected).

Any ideas?

Regards,
SueB

*** Sent via Developersdex http://www.developersdex.com ***
Nov 13 '05 #5

P: n/a
If you have two listboxes, lst1 and lst2 and their MultiSelect
properties are set to either simple or extended, use their AfterUpdate
events to disable the listbox you want to exclude, e.g.:

Private Sub lst1_AfterUpdate()

If lst1.ItemsSelected.Count > 0 Then
lst2.Enabled = False
Else
lst2.Enabled = True
End If

End Sub

Private Sub lst2_AfterUpdate()

If lst2.ItemsSelected.Count > 0 Then
lst1.Enabled = False
Else
lst1.Enabled = True
End If

End Sub

To iterate through the items the user actually selected when they click
the command button use something like the following code (note that
this shows only what is in the bound column of the enabled listbox):

Dim lst As ListBox
Dim var As Variant
Dim strItems As String

If lst1.Enabled Then
Set lst = lst1
Else
Set lst = lst2
End If

For Each var In lst.ItemsSelected
strItems = strItems & lst.ItemData(var) & ","
Next

MsgBox "You selected items " & strItems

If you need to use the value of a column other than the bound column
from the enabled listbox use the column property instead of the
ItemData property. For example, if you wanted to use the value in the
third column of the enabled listbox rather than the bound column,
replace

lst.ItemData(var)

with:

lst.Column(2,var)

in the above code (note that columns are 0 based so the 2 above refers
to the 3rd column).

HTH,
Bruce

Nov 13 '05 #6

P: n/a
A global Thank you to all who replied. I'm getting lots of good
suggestions and learning along the way. Thanks, again.

Regards,
SueB

*** Sent via Developersdex http://www.developersdex.com ***
Nov 13 '05 #7

P: n/a
SueB wrote:
Hey, Salad...

Is there a way to "unselect" items in a listbox, programmatically?
Here's what I want to (and I've been trying to to this while debugging
the form, but I've been unsuccessful) ...
Sure

Let's say you have a multiselect listbox. And you want all items
selected to be set to unselected. In some event you could have code
that looks like this
Dim varItem As Variant
For Each varItem In Me.ListBoxName.ItemsSelected
Me.ClientList.Selected(varItem) = False
Next varItem

I have the option buttons in an options group: 1 button for the first
listbox and 1 button for the otgher listbox.

Let's say the user selects optionbutton 1 and enables the first listbox.
Then they select a few items in the first listbox.

Then they decide (before hitting the command button to generate the
query) that they really want to use the 2nd list box. So, they hit the
other option button, which will disable listbox 1 and enable listbox 2.
However, the items that were selected in listbox 1 are still
highlighted. I want to get those highlighted items unhighlighted
(unselected).
I would clear out the selections for both listboxes when you go to a new
record. Or else have a command button to clear out the selected
entries. I would not clear it out by selecting which listbox to use
(see clear out code above)

I can see your dilemma but I'd not do that. I try, as much as possible,
to have sympathy and empathy for the poor user. If I can do as much as
possible to fill in values or make their keystrokes less then I feel I
succeed.

So if a user selects ListBox2, and has selected the items from ListBox1,
and now decides ListBox1 is the right one, if you've cleared it out then
the person needs to reselect all of the items.

Let's say you have 2 listboxes; Listbox1/2. Lets say you have an option
group (Frame1) with 2 checkboxes, you could do something like this
Me.ListBox1.Enabled = (Me.Frame1 = 1)
Me.ListBox2.Enabled = (Me.Frame1 = 2)
based upon the selection.
Now, you have a command button to press when the selections are pressed.
And your code does something like
Dim strSQL As String
If Me.Frame1 = 1 then
strSQL = ...
Else
strSQL = ....
endif
...process something
Any ideas?

Regards,
SueB

*** Sent via Developersdex http://www.developersdex.com ***

Nov 13 '05 #8

P: n/a
Well...I'm not Salad, but I like to keep things simple too...

If your listbox's MultiSelect property is set to Extended, simply
requerying the listbox will clear it, e.g.

lst1.Requery
If set to Simple, the code Salad provided works well.

HTH,
B

Nov 13 '05 #9

P: n/a
Thanks for the simple approach. I'm all for that.

Regards,
SueB

*** Sent via Developersdex http://www.developersdex.com ***
Nov 13 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.