List Box Deselect Issue

J

JoyEakins

Hi -

I have a listbox that allows the user to pick which product to show in a
report. It is Extended MultiSelect. The user picks multiple items, runs the
report and then closes the report. They can click a button to "clear the
listbox" and then pick new products. However when they click on the listbox
again - if they click something that was previously selected, it will not
select the item until the user clicks it at least 2 times. Any advice to
stop this behavior? Below is the code I'm using with the clear button.

for each vitem in lstcontrol.itemselected
.selected(vitem)=false
next

Thanks!
Joy
 
D

Dirk Goldgar

JoyEakins said:
Hi -

I have a listbox that allows the user to pick which product to show in a
report. It is Extended MultiSelect. The user picks multiple items, runs
the
report and then closes the report. They can click a button to "clear the
listbox" and then pick new products. However when they click on the
listbox
again - if they click something that was previously selected, it will not
select the item until the user clicks it at least 2 times. Any advice to
stop this behavior? Below is the code I'm using with the clear button.

for each vitem in lstcontrol.itemselected
.selected(vitem)=false
next


I use the following code, and it doesn't seem to have that problem:

Dim intI As Integer

With Me.lstControl

For intI = (.ItemsSelected.Count - 1) To 0 Step -1
.Selected(.ItemsSelected(intI)) = False
Next intI

.SetFocus
.ListIndex = Abs(.ColumnHeads)

End With

I'm using Access 2003, if it makes any difference. I'd expect this same
code to work fine in Access 2007, but I don't recall if I've tried it.
 
D

Dirk Goldgar

Dirk Goldgar said:
I use the following code, and it doesn't seem to have that problem:

Dim intI As Integer

With Me.lstControl

For intI = (.ItemsSelected.Count - 1) To 0 Step -1
.Selected(.ItemsSelected(intI)) = False
Next intI

.SetFocus
.ListIndex = Abs(.ColumnHeads)

End With

I'm using Access 2003, if it makes any difference. I'd expect this same
code to work fine in Access 2007, but I don't recall if I've tried it.


Hmm, when I test with the Multiselect property set to Extended, rather than
Simple, my code gives an unwanted effect: The line
.ListIndex = Abs(.ColumnHeads)

.... appears to select the first item in the list, which is not what happens
with Simple multiselect. If you use my suggested code, you probably want to
remove that line.

On the other hand, when I try your code -- after making minor corrections --
I don't see the behavior you report. Would you care to post your full and
exact code via copy & paste, rather than a transcription of it? Is there
code in any other events of the list box that might be interfering?
 
J

JoyEakins

Thanks Dick. Happy to repost the code. I am calling a subroutine because
there are 6 listboxes on the form that need to be cleared out. I am using
Access 2007. There are not events in the form - just 4 buttons - 2 run
reports, 1 closes the form and 1 clears all listboxes. Here is the code:

Private Sub cmdClearForm_Click()
Call ClearList(Me.lstAuthor)
Call ClearList(Me.lstCase)
Call ClearList(Me.lstDocType)
Call ClearList(Me.lstKeywords)
Call ClearList(Me.lstProduct)
Call ClearList(Me.lstRecipient)

Me.txtEndDate = Null
Me.txtEndDoc = Null
Me.txtStartDate = Null
Me.txtStartDoc = Null

End Sub

Sub ClearList(lstControl As ListBox)

Dim vItem As Variant

For Each vItem In lstControl.ItemsSelected
lstControl.Selected(vItem) = False
Next


End Sub

Any help you can provide would be super. I also tried using a SetFocus in the
ClearList subroutine and it didn't work. I've used Access for 100's of db's
and this is the first time I recall having this issue.

Thanks,
Joy




Dirk said:
[quoted text clipped - 29 lines]
I'm using Access 2003, if it makes any difference. I'd expect this same
code to work fine in Access 2007, but I don't recall if I've tried it.

Hmm, when I test with the Multiselect property set to Extended, rather than
Simple, my code gives an unwanted effect: The line
.ListIndex = Abs(.ColumnHeads)

... appears to select the first item in the list, which is not what happens
with Simple multiselect. If you use my suggested code, you probably want to
remove that line.

On the other hand, when I try your code -- after making minor corrections --
I don't see the behavior you report. Would you care to post your full and
exact code via copy & paste, rather than a transcription of it? Is there
code in any other events of the list box that might be interfering?
 
D

Dirk Goldgar

JoyEakins said:
Thanks Dick. Happy to repost the code. I am calling a subroutine because
there are 6 listboxes on the form that need to be cleared out. I am using
Access 2007. There are not events in the form - just 4 buttons - 2 run
reports, 1 closes the form and 1 clears all listboxes. Here is the code:

Private Sub cmdClearForm_Click()
Call ClearList(Me.lstAuthor)
Call ClearList(Me.lstCase)
Call ClearList(Me.lstDocType)
Call ClearList(Me.lstKeywords)
Call ClearList(Me.lstProduct)
Call ClearList(Me.lstRecipient)

Me.txtEndDate = Null
Me.txtEndDoc = Null
Me.txtStartDate = Null
Me.txtStartDoc = Null

End Sub

Sub ClearList(lstControl As ListBox)

Dim vItem As Variant

For Each vItem In lstControl.ItemsSelected
lstControl.Selected(vItem) = False
Next


End Sub

Any help you can provide would be super. I also tried using a SetFocus in
the
ClearList subroutine and it didn't work. I've used Access for 100's of
db's
and this is the first time I recall having this issue.


Are all the list boxes set to Extended MultiSelect?
 
J

JoyEakins

Yes. They are all Extended MultiSelect. Thanks for asking.

Dirk said:
Thanks Dick. Happy to repost the code. I am calling a subroutine because
there are 6 listboxes on the form that need to be cleared out. I am using
[quoted text clipped - 31 lines]
db's
and this is the first time I recall having this issue.

Are all the list boxes set to Extended MultiSelect?
 
D

Dirk Goldgar

JoyEakins said:
Yes. They are all Extended MultiSelect. Thanks for asking.


I've been testing with Access 2007, where I can confirm your experience.
This appears to be a bug in the display of the list box. After you have
cleared the selection, whether using your code or mine, if you then go an
click on one of the rows that was previously selected, that row really is
now selected -- BUT it doesn't look like it. You can demonstrate that the
row is actually selected by checking the listbox's ItemsSelected collection,
but the row is not highlighted properly.

If you invalidate that portion the form window, by hiding it and then
redisplaying it, or by minimizing it and then restoring it, or even by
dragging another form across it, then the row magically appears selected as
it ought.

I'll report this bug to Microsoft and see what they say. In the mean time,
one (ugly) workaround is to have a module level variable to flag whether you
have cleared the list box this way, and use the list box's Click event to
check that variable and, if the list box was previously cleared, toggle the
form's Visible property off and on. That works, but is incomplete. Also,
you can't really use it in the context of a general-purpose function to
clear any of a number of list boxes.
 
J

JoyEakins

Dirk - thanks for checking this out and verifying. I thought it was a bug in
Access too. For now I think I'll just see if I can convince my client to
click on the highlighted item twice, since that is a valid workaround (though
ugly). But if Microsoft would be willing to fix it at some point or give a
better workaround, I would be very interested.

Appreciate all your time on this today!
Joy
 
D

Dale Fye

Joy,

I've used similar code for years, and never paid any attention to this,
maybe because it didn't happen in 2003??

I've tried your code, Dirk's code and a couple of variants. The only think
that seems to work is to just requery the list, which of course makes the
fires item in the rowsource the first item displayed in the list.

I'm using a list that contains the number 1-10 as my list items. Regardless
of whether I use your code or Dirk's, if after I click cmdClear, I select an
item from the list that was not previously selected, it highlights it
correctly with the first click. If I highlight one of the items that was
previously selected, it requires 2 clicks.

Also, if I highlight a previously unselected item, and drag over some of the
previously selected items, those that were previously selected appear to be
selected, but the they have a dark border and text that is black on white,
instead of white on black.

--
----
HTH
Dale



JoyEakins said:
Thanks Dick. Happy to repost the code. I am calling a subroutine because
there are 6 listboxes on the form that need to be cleared out. I am using
Access 2007. There are not events in the form - just 4 buttons - 2 run
reports, 1 closes the form and 1 clears all listboxes. Here is the code:

Private Sub cmdClearForm_Click()
Call ClearList(Me.lstAuthor)
Call ClearList(Me.lstCase)
Call ClearList(Me.lstDocType)
Call ClearList(Me.lstKeywords)
Call ClearList(Me.lstProduct)
Call ClearList(Me.lstRecipient)

Me.txtEndDate = Null
Me.txtEndDoc = Null
Me.txtStartDate = Null
Me.txtStartDoc = Null

End Sub

Sub ClearList(lstControl As ListBox)

Dim vItem As Variant

For Each vItem In lstControl.ItemsSelected
lstControl.Selected(vItem) = False
Next


End Sub

Any help you can provide would be super. I also tried using a SetFocus in the
ClearList subroutine and it didn't work. I've used Access for 100's of db's
and this is the first time I recall having this issue.

Thanks,
Joy




Dirk said:
[quoted text clipped - 29 lines]
I'm using Access 2003, if it makes any difference. I'd expect this same
code to work fine in Access 2007, but I don't recall if I've tried it.

Hmm, when I test with the Multiselect property set to Extended, rather than
Simple, my code gives an unwanted effect: The line
.ListIndex = Abs(.ColumnHeads)

... appears to select the first item in the list, which is not what happens
with Simple multiselect. If you use my suggested code, you probably want to
remove that line.

On the other hand, when I try your code -- after making minor corrections --
I don't see the behavior you report. Would you care to post your full and
exact code via copy & paste, rather than a transcription of it? Is there
code in any other events of the list box that might be interfering?

.
 
M

Marshall Barton

Dirk said:
If you invalidate that portion the form window, by hiding it and then
redisplaying it, or by minimizing it and then restoring it, or even by
dragging another form across it, then the row magically appears selected as
it ought.


Dirk, does Me.Repaint straighten out the display?
 
D

Dirk Goldgar

Marshall Barton said:
Dirk, does Me.Repaint straighten out the display?


No, that was the first thing I thought of, but it doesn't. My guess is that
one could call a Windows API call to invalidate the listbox's window and
force the system to repaint it. But I didn't have time to get that deep
into it. Hiding/showing, minimizing/restoring, and just dragging another
form over it, all work.
 
M

Marshall Barton

Dirk said:
in message


No, that was the first thing I thought of, but it doesn't. My guess is that
one could call a Windows API call to invalidate the listbox's window and
force the system to repaint it. But I didn't have time to get that deep
into it. Hiding/showing, minimizing/restoring, and just dragging another
form over it, all work.


That seems odd to me. Maybe it has something to do with the
list box not having the focus?? If I remember correctly, a
control is not a window unless it has the focus.

I also have a vague memory of multiselect list boxes being
totally reset with:
Me.listbox.RowSource = Me.listbox.RowSource
 
D

Dirk Goldgar

Marshall Barton said:
That seems odd to me. Maybe it has something to do with the
list box not having the focus?? If I remember correctly, a
control is not a window unless it has the focus.

Most controls, yes. I think it's different for list boxes, but I can't say
I'm sure.
I also have a vague memory of multiselect list boxes being
totally reset with:
Me.listbox.RowSource = Me.listbox.RowSource

That would probably work. I know it will reset the selections, at the cost
of the extra time to re-execute the rowsource query. I'll have to check
whether it resolves the problem.
 

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