469,903 Members | 1,760 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,903 developers. It's quick & easy.

Unselecting items in a Listbox (HOW?)

Hi.

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

I have a form with two listboxes.

The form, also, has two option buttons in an options group: 1 button for
the first
listbox and 1 button for the otgher listbox. The idea is to select an
optionbutton to enable the associated listbox. This is working just
fine. However ...

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 - which is the purpose of the form) 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 #1
10 20034
SueB wrote in message <OI**************@news.uswest.net> :
Hi.

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

I have a form with two listboxes.

The form, also, has two option buttons in an options group: 1 button for
the first
listbox and 1 button for the otgher listbox. The idea is to select an
optionbutton to enable the associated listbox. This is working just
fine. However ...

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 - which is the purpose of the form) 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 ***


For multiselect listboxes, I believe one way is to reassigning the
rowsource

me!lstList.rowsource = me!lstList.rowsource

Else I think you could loop the list, and set each item

for l = 0 to me!lstList.listcount-1
me!lstList.selected(l) = false
next l

--
Roy-Vidar

Nov 13 '05 #2

Sue -

Try this (modified appropriately to fit your code):
Private Sub btnUseList2_Click()
Dim lngLoop As Long

lst1.Enabled = False
For lngLoop = 0 To lst1.ItemsSelected.Count - 1
lst1.Selected(lst1.ItemsSelected.Item(lngLoop)) = False
Next lngLoop
lst2.Enabled = True
End Sub

This code is run when the button to enable the second list box is clicked; it
does the following:
1) Disables the first list box
2) Cycles through the items in the first list box, de-selecting any that are
selected
3) Enables the second list box

Hope this helps!

Peter

SueB wrote:
Hi.

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

I have a form with two listboxes.

The form, also, has two option buttons in an options group: 1 button for
the first
listbox and 1 button for the otgher listbox. The idea is to select an
optionbutton to enable the associated listbox. This is working just
fine. However ...

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 - which is the purpose of the form) 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

Nov 13 '05 #3
Roy,

Thank you for the quick reply. I'll give both methods a try and see
which works best for me.

Regards,
SueB

*** Sent via Developersdex http://www.developersdex.com ***
Nov 13 '05 #4
Maybe it is better if your code does take the option button into
account. What if I think again, and revert to listbox 1? I find all my
selections gone.

That said, you have to set the Selected(row) to False, for every row
that is selected.

dim item as variant
for each item in yourlistbox.itemsselected
yourlistbox.selected(item) = false
next

SueB wrote:
Hi.

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

I have a form with two listboxes.

The form, also, has two option buttons in an options group: 1 button for
the first
listbox and 1 button for the otgher listbox. The idea is to select an
optionbutton to enable the associated listbox. This is working just
fine. However ...

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 - which is the purpose of the form) 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 ***


--
Bas Cost Budde, Holland
http://www.heuveltop.nl/BasCB/msac_index.html
For human replies, replace the queue with a tea

Nov 13 '05 #5
I'm sending out a global THANK YOU to all who replied. Wow. There
certainly look there are few different ways to approach this. I'll go
check out all the options and see what fits. Thanks again.
Regards,
SueB

*** Sent via Developersdex http://www.developersdex.com ***
Nov 13 '05 #6
Just in case anybody else looks at this post...I discovered a slight
flaw with what was suggested above. When you loop through the selected
items in the list and use the itemsselected.count as the upper boundary
of the loop without saving that value initially and using the saved
value the count reduces with every loop (as you iterate through the
items). Also, the index value of the selected items changes if you loop
from 0 up to the total selected count. So ... here is what I used to
get around the problem (I loop from the last item back up to the first
item)...

Public Sub clrSelectedDates()

Dim ix As Long
Dim lngCount As Long

'Save the selected count
lngCount = Me!lstProcesseddt.ItemsSelected.Count - 1

'Loop from the end to the beginning of the selected
For ix = lngCount To 0 Step -1

Me!lstProcesseddt.Selected(Me!lstProcesseddt.Items Selected.Item(ix)) =
False
Next ix

End Sub

Regards,
SueB

*** Sent via Developersdex http://www.developersdex.com ***
Nov 13 '05 #7
Susan Bricker <sl*****@verizon.net> wrote:
: Just in case anybody else looks at this post...I discovered a slight
: flaw with what was suggested above. When you loop through the selected
: items in the list and use the itemsselected.count as the upper boundary
: of the loop without saving that value initially and using the saved
: value the count reduces with every loop (as you iterate through the
: items). Also, the index value of the selected items changes if you loop
: from 0 up to the total selected count. So ... here is what I used to
: get around the problem (I loop from the last item back up to the first
: item)...

Would looping with a 'for each' statement, where you never
need to specify the array index avoid this problem?
--thelma
: Regards,
: SueB

Nov 13 '05 #8
No, If you loop through a collection removing items from teh collection you
should always loop downwards as Susan has discovered.

Terry Kreft
Microsoft Access MVP
"Thelma Lubkin" <th****@alpha2.csd.uwm.edu> wrote in message
news:dc**********@uwm.edu...
Susan Bricker <sl*****@verizon.net> wrote:
: Just in case anybody else looks at this post...I discovered a slight
: flaw with what was suggested above. When you loop through the selected
: items in the list and use the itemsselected.count as the upper boundary
: of the loop without saving that value initially and using the saved
: value the count reduces with every loop (as you iterate through the
: items). Also, the index value of the selected items changes if you loop
: from 0 up to the total selected count. So ... here is what I used to
: get around the problem (I loop from the last item back up to the first
: item)...

Would looping with a 'for each' statement, where you never
need to specify the array index avoid this problem?
--thelma
: Regards,
: SueB

Nov 13 '05 #9
Or,

with yourcollection
do while .count>0
.remove 0
loop
end with

Terry Kreft wrote:
No, If you loop through a collection removing items from teh collection you
should always loop downwards as Susan has discovered.


Nov 13 '05 #10

Susan -

Whoops! Sorry, I missed that. My bad.

Definitely, looping backwards is the right way to do it. I do not think,
however, you have to store the initial count in a variable.

I believe Access evaluates the loop expression only once, so even though the
count is changing, the loop statement is set in its path. You can prove this
with the following code:
lngCount = 1
For lngLoop = lngCount To 5
lngCount = 5
Debug.Print lngLoop
Next lngLoop

The loop will still have 5 iterations. Even though lngCount gets changed
inside the loop, Access has already evaluated the expression and begun the
loop. So, your code could be rewritten as follows:
Public Sub clrSelectedDates()
Dim ix As Long

'Loop from the end to the beginning of the selected
For ix = (Me!lstProcesseddt.ItemsSelected.Count - 1) To 0 Step -1
Me!lstProcesseddt.Selected(Me!lstProcesseddt.Items Selected.Item(ix))
= False
Next ix
End Sub

Note that if you were using a DO WHILE or DO...UNTIL loop, the expression
would get evaluated every time, but FOR...NEXT loops are only evaluated once.

Peter Schroeder

Susan Bricker wrote:
Just in case anybody else looks at this post...I discovered a slight
flaw with what was suggested above. When you loop through the selected
items in the list and use the itemsselected.count as the upper boundary
of the loop without saving that value initially and using the saved
value the count reduces with every loop (as you iterate through the
items). Also, the index value of the selected items changes if you loop
from 0 up to the total selected count. So ... here is what I used to
get around the problem (I loop from the last item back up to the first
item)...

Public Sub clrSelectedDates()

Dim ix As Long
Dim lngCount As Long

'Save the selected count
lngCount = Me!lstProcesseddt.ItemsSelected.Count - 1

'Loop from the end to the beginning of the selected
For ix = lngCount To 0 Step -1

Me!lstProcesseddt.Selected(Me!lstProcesseddt.Item sSelected.Item(ix)) =
False
Next ix

End Sub

Regards,
SueB

--
Message posted via AccessMonster.com
http://www.accessmonster.com/Uwe/For...ccess/200507/1
Nov 13 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Jason P Opdycke [MSFT] | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.