Listbox returning last clicked item not highlighted (1 Viewer)

Rachael

Registered User.
Local time
Today, 19:12
Joined
Nov 2, 2000
Messages
205
Hi All,

Can't figure out what the hell I'm doing wrong here!!! :banghead:

I have 2 multi-select listboxes set to simple
The first is use to select a vineyard and the second to display the blocks associated with that vineyard - can get this to work fine - no probs.

What I want to do however is if more than one vineyard is selected the second listbox is hidden as selecting more than one vineyard assumes all blocks selected - this is all good too!

Problem occurs when the user un-clicks a vineyard to go back to displaying only one, therefore unhiding blocks listbox and populating with the correct blocks. My code seems to run on the last clicked item not the item left highlighted

Here's my code:

Private Sub LstCo_AfterUpdate()
Dim varItem1 As Variant
Dim LstCo1 As Control

Set LstCo1 = Me.LstCo

If LstCo1.ItemsSelected.Count = 1 Then
For Each varItem1 In LstCo1.ItemsSelected

MsgBox LstCo1.Column(1) 'display vineyard name for debugging

Me.txtCoID = LstCo1.Column(0)

Me.LstBlk.Visible = True
Me.LstBlk.Requery
Next varItem1

Else
Me.txtCoID = Null

If LstCo1.ItemsSelected.Count > 1 Then
Me.LstBlk.Visible = False

Else
Me.LstBlk.Requery
End If

End If
End Sub


All help greatly appreciated, thank you!

Rachael
 

OriOn

New member
Local time
Today, 20:12
Joined
Apr 5, 2017
Messages
7
Hi,
by clicking on an item of the listbox you 'select' it. After, the item is selected or not depending of the previous status.
The focus is then set on the clicked item (see a dashed rectangle around it).

The method used is then not the good one in this situation.
By chance there are others :) here is a work around for you.

Code:
...
If LstCo1.ItemsSelected.Count = 1 Then
' replace this loop
'    For Each varItem1 In LstCo1.ItemsSelected
'        Debug.Print LstCo1.Column(1) 'display vineyard name for debugging
'    Next varItem1

' by one like this    
    For i = 0 To LstCo1.ListCount
            If LstCo1.Selected(i) Then
                Debug.Print LstCo1.ItemData(i)
            End If
    Next
End If

of course instead of printing it in the debug you collect the value and play with it.

have fun!
 

Rachael

Registered User.
Local time
Today, 19:12
Joined
Nov 2, 2000
Messages
205
Ok so light bulb moment!!

Me.txtCoID = LstCo1.Column(0)

should have been

Me.txtCoID = LstCo1.Column(0, varItem1)

ddeeerrrrrrrrrrr what a dick....
 

Users who are viewing this thread

Top Bottom