Deselect last item selected in multi select listbox

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

How do I deselect the last item selected in a multi select listbox?

I have tried various iterations of code with no success:

Mr B
 
The last chronologically, or the last sequentially?

For sequentially, you can use:

Me.List0.Selected(Me.List0.ItemsSelected.Item(Me.List0.ItemsSelected.Count -
1)) = False

(replace List0 with the actual name of your listbox)

For chronologically, there's nothing built into Access that I'm aware of.
You might be able to put code in the list box's Click event to determine the
order in which the entries were selected, but it would be messy.
 
Doug,

Thanks for the reply.

Unfortunately, it is the chronological selection that I am looking for.

A more indepth description of what I am attempting to do is:

I have a list of items that are about to be added to second list (multiple
selections). However, as the user makes a selection, I am checking the bound
value to see if that selection is already in the second list. If it is, I am
notifying the user that the selection is already in the list. I just need to
unselect the item they selected to trigger the After Update event if the list
box.

I do already have the bound column value for the selected item. Can I some
way use that to return the selecteditem value and unselect it? (Hope that was
clear enough.)


Mr B
 
If you're already checking whether or not a particular selection exists in
the second list, just set its Selected property to False.
 
I know it sounds as if that is all I would have to do. Just set its Selected
property to false. However, the way I'm trying to do it rigth now, it seems
to work for a single selection, but not if another selection was made first.

Here is the code that I am trying to use:

'read the ProductID for the selected Product
Dim lngProductID As Long
lngProductID = Me.lstProducts.Column(0)
Dim strMsg As String
Dim varSelCnt
'check to see if that Product is already on the list
varRecCnt = DCount("ItemID", "tblNextList", "ProductID = " & lngProductID &
"")
If varRecCnt > 0 Then
varSelCnt = Me.lstProducts.ItemsSelected.Count
If varSelCnt > 0 Then
strMsg = "That produce it already in the Next List"
MsgBox strMsg, vbInformation + vbOKOnly, "Duplicated Product"
Me.lstProducts.Selected(varSelCnt) = False
End If
End If

I think I may need to try to hold on to a different value than the
ItemsSelected.count but I am not sure just what I do need to try.

Thanks for any additional help.

Mr B
 
Just thought I'd post back to let you know that I finally got it to work as I
needed it to. Below is the code that finally did the job.

'read the ProductID for the selected Product
Dim lngProductID As Long
lngProductID = Me.lstProducts.Column(0)
Dim strMsg As String
Dim varSelCnt
Dim frm As Form
Dim ctl As Control
Dim varItm As Variant
'check to see if that Product is already on the list
varRecCnt = DCount("ItemID", "tblNextList", "ProductID = " & lngProductID &
"")
If varRecCnt > 0 Then
varSelCnt = Me.lstProducts.ItemsSelected.Count
If varSelCnt > 0 Then
strMsg = "That produce it already in the Grocery List"
MsgBox strMsg, vbInformation + vbOKOnly, "Duplicated Product"
'Me.lstProducts.Selected(varSelCnt) = False
'add all selected products to the Grocery List
Set frm = Forms!frmNeedsList
Set ctl = frm!lstProducts
For Each varItm In ctl.ItemsSelected
If ctl.ItemData(varItm) = lngProductID Then
Me.lstProducts.Selected(varItm) = False
End If
Next varItm
End If
End If

I actaully had to iterate through each of the items in the list, checking
each item to see if that was the item that contained the ProductID that I had
captured to a variable in the OnClick event of the control. The code that
really does the trick is:

For Each varItm In ctl.ItemsSelected
If ctl.ItemData(varItm) = lngProductID Then
Me.lstProducts.Selected(varItm) = False
End If
Next varItm

The only reason that I posted this back was so that in the event that anyone
else every needed to accomplish something like this, perhaps they would not
have to spend as much time figuring it out as I did.

Doug, thanks so much for all your help. You have helped me many times. Just
doing a search in the newsgroups for something I need to know about will most
times help me accomplish the task. Your postings, along with the many others
who attempt to help with their postings, are really appreciated by many of us
who frequent these newsgroups.

Mr B
 
Back
Top