Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MS Access Determine the listbox item clicked on click event

I have a ListBox within MS Access and would like to find the best way to get the list item which was selected/deselected at the time of the on-click event.

Its a little more complicated than looping through the selected items, since the listbox is already loaded with some items selected. I am trying to find the single item which was affected at the time of the on-click event.

enter image description here

So, If the user clicks "Col2-How" in the example above, how would I determine that was the record clicked, Alternatively, if one deselects the first record, I would need to know. Any clues?

The only thing I can think of is to use an in-memory object to maintain alist of highlighted rows and track back to the selected items at the time of the click to determine the deltas?

like image 226
Hightower Avatar asked Dec 06 '22 00:12

Hightower


1 Answers

you could use AfterUpdate event, for example the name of the listbox is 'aListbox', so try this :

Private Sub aListBox_AfterUpdate()
  Dim rowIndex As Integer
  Dim rowValue As String
  Dim rowIsSelected As Integer
  Dim result As String

  ' ListBox row index clicked
  rowIndex = Me.aListBox.ListIndex

  ' Row value clicked
  rowValue = Me.aListBox.Column(0)

  ' If row is selected return value is -1, if unselected return value 0
  rowIsSelected = Me.aListBox.Selected(rowIndex)

  If (rowIsSelected = -1) Then
    result = rowValue & " selected"
  Else
    result = rowValue & " unselected"
  End If

  MsgBox (result)

End Sub
like image 144
Jérôme Teisseire Avatar answered Mar 13 '23 04:03

Jérôme Teisseire