473,725 Members | 2,232 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4949
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.Item sSelected
'this gets the value of the first column
varValue = Me.ListBox.Item Data(varItem)
Next varItem

For Each varItem In Me.ListBox.Item sSelected
'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.Item Data(varItem).C olumn(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...al ways 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, programmaticall y?
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_AfterUpdat e()

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

End Sub

Private Sub lst2_AfterUpdat e()

If lst2.ItemsSelec ted.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.ItemsSelect ed
strItems = strItems & lst.ItemData(va r) & ","
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(va r)

with:

lst.Column(2,va r)

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, programmaticall y?
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.S elected(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.Ena bled = (Me.Frame1 = 1)
Me.ListBox2.Ena bled = (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
2417
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 writing a usercontrol which displays the typical two listboxes and the ability to move items from one to the other. The listboxes are populated with my custom objects (SwapItem), which simply
3
1708
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: data records are A and B, and initially are available in both listboxes, but once the user selects A in the first listbox, the second one should only display B. Any reference?
2
1504
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 form, I got two listboxes populated with the .name and the .ID from the two tables.
2
2608
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 shows equipment inventory at b OriginalVAL= query that extracts original $ amount from INV table based on matching EquipmentID seleced in C (currency)(listbox)(unbound). BTA = query that calculates total amount for all $ transactions for...
4
1913
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 AutoPostBack="False" BackColor="#ffffff" id="lstCrewChief" runat="server" Rows="1" DataSource="<%# DsCrewChief1 %>" Enabled="True"
0
1099
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 fields in the grid. There are 10 columns in the grid, 5 static fields (labels), 4 listboxes, and 1 edit box. 2 of the listboxes hold about 12 items each and the other 2 listboxes only contain 2 items. The problem is that on some lower end...
1
1347
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 arrLookup = new Array(); var i; for (i = 0; i < tbox.options.length; i++) {
3
2481
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 and Listbox2. Listbox1 contains some names and listbox2 contains nothing. if I doubleclick Listbox1 all names in that listbox will be transferred to listbox2 (Listbox1 will now be empty and Listbox2 filled)...the next thing is...Solution 1: if I...
6
4403
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, Dominic Isaia disaia@spacecraft.com
5
2451
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 filter on 5 fields in this table, and can include as many field values as s/he needs. The popup is reached from a command button on the main form : This button is on the main form, Datasystem:
0
8888
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9401
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9257
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9176
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8097
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6702
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4519
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3221
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.