473,395 Members | 1,440 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Using Listboxes to enable multi-select



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
9 4918
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
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
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
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
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
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
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
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
Thanks for the simple approach. I'm all for that.

Regards,
SueB

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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Alex Stevens | last post by:
I'd already posted this in microsoft.public.dotnet.framework.windowsforms and microsoft.public.dotnet.framework.windowsforms.controls to no avail so apologies for the cross-posting. Hi, I'm...
3
by: Simon Templar | last post by:
I need the following functionality: With 2 listboxes populated from a database with the SAME data, I need any of the listboxes to stop displaying the option when selected at the other listbox. Eg:...
2
by: demo | last post by:
Hi I got two tables, people and events. Each have a .name and a .ID. In a third table, participation, I have a many to many connection between the two, conection people to events. In a...
2
by: Scot Niklos via AccessMonster.com | last post by:
Sorry if it is way to much information but I'm trying to cover all bases Access 97 A= combobox that queries Distributor B= combobox that limits customers by distributor in a C= combobox that...
4
by: bill yeager | last post by:
I have several template columns inside of a datagrid. Inside of these template columns are databound listboxes: <asp:TemplateColumn HeaderText="Crew Chiefs"> <ItemTemplate> <asp:listbox...
0
by: Terry D | last post by:
I'm having an issue with an ASP.NET page (VS.NET 2003, VB.NET, Oracle back end). The page uses the standard VS.NET grid to display the records from a particular table. The user can edit certain...
1
by: Ryan Ternier | last post by:
I have two listboxes, and allow users to move items between them via the following function: function SwitchList(fbox, tbox){ var arrFbox = new Array(); var arrTbox = new Array(); var...
3
by: Dag | last post by:
Hello there! I have a problem here. For many of you it would not be a problem at all, but I am new in programming...so here is the question: I have a form with two listboxes in it....Listbox1...
6
by: Dominic Isaia | last post by:
I am trying to simulate a multicolumn listbox. I need a way to link two listboxes together so that if you scroll one listbox the other scrolls with it. Any help will be appreciated. Thanks,...
5
by: Thelma Roslyn Lubkin | last post by:
I am still having trouble trying to use a popup form to allow user to set filters for the main form. The main form is based on a single table. The popup contains 5 listboxes, so the user can...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.