Last selected item on list box

aziz rasul

Active member
Local time
Today, 16:48
Joined
Jun 26, 2000
Messages
1,935
Does anyone have any code that will give me the value of the last selected item in a listbox where the MultiSelect property of the list box is set to 'Simple'. Once I get this value I want to deselect all chosen items except the last one selected.

I can resolve the issue by selecting 'None' in the MultiSelect property of the list box. However when I do this I get the vertical scroll bar positioning itself anywhere. If I set the MultiSelect property of the list box to 'Simple' then this doesn't happen.
 
Last edited:
You can deselect all but the last by iterating through backwards

So first, if you want the last one selected to stay, create a variable like this in the form's general declarations section

Code:
Option Compare Database
Option Explicit
Private lngLastSelected As Long

And then in the Click event of the list box (replace List0 with your real name):
Code:
lngLastSelected = Me.List0.Column(0)

And last, the code for the event being used to deselect all but the last one selected:
Code:
    Dim intCount As Integer
    For intCount = Me.List0.ItemsSelected.Count - 1 To 0 Step -1
        If Me.List0.ItemData(Me.List0.ItemsSelected(intCount)) <> lngLastSelected Then
            Me.List0.Selected(Me.List0.ItemsSelected(intCount)) = False
        End If
    Next
 

Users who are viewing this thread

Back
Top Bottom