Scan Through Multi-Select Listboxes And Display Non-Bound Columns
In a multi-select List Box, the developer needs to scan through all selections. The Bound column data is presented through the ItemData property, but to show other columns in the same listbox requires the use of the Column property. The demo below shows how to both scan through a listbox for selections and also display non-bound columns.
Program Code
Option Compare Database Option Explicit Private Sub cmdClearAllSelection_Click() Call ClearAll End Sub Private Sub cmdSelectAll_Click() Call SelectAll End Sub ' ****************************************************************** ' This Demonstrates How To Loop Through Selections of a List Box ' And Display the Column Data That is Not Bound ' ****************************************************************** Private Sub cmdProceedWithPurge_Click() Dim varSelectedClientID As Variant Dim lngClientIDToBePurged As Long Dim strClientNameToPurge As String Dim lngResponse As Long ' ****************************************************************** ' Determine if All or Selected Purge Was Requested ' ****************************************************************** If Me.frameSelectOptions = 2 Then lngResponse = MsgBox("Are You Sure You Want To Purge All Clients?", vbYesNo) If lngResponse = vbNo Then MsgBox ("Client Data Not Purged") Exit Sub End If MsgBox ("All Client Data Will Be Purged") ' Put Purge Process Here Exit Sub End If ' ****************************************************************** ' Loop Through Purge Selections ' ****************************************************************** If Me.lstClientsSelected.ItemsSelected.count = 0 Then MsgBox ("You Must Select At Least 1 Client To Be Purged") Exit Sub End If With Me.lstClientsSelected For Each varSelectedClientID In .ItemsSelected If Not IsNull(varSelectedClientID) Then lngClientIDToBePurged = .ItemData(varSelectedClientID) lngResponse = MsgBox("Are You Sure You Want To Purge " & _ Me.lstClientsSelected.Column(0, varSelectedClientID) & "?", vbYesNo) If lngResponse = vbYes Then MsgBox ("Purging " & Me.lstClientsSelected.Column(0, varSelectedClientID)) End If End If Next End With End Sub Private Sub ClearAll() Dim varClientID As Variant For Each varClientID In Me.lstClientsSelected.ItemsSelected Me.lstClientsSelected.Selected(varClientID) = False Next End Sub Private Sub SelectAll() Dim lngRow As Long For lngRow = 0 To Me.lstClientsSelected.ListCount - 1 Me.lstClientsSelected.Selected(lngRow) = True Next End Sub