Private Function GetListValues() As Variant
Dim sVal$, idx%
For idx = 0 To Me.lstDefects.ListCount - 1
If Me.lstDefects.Selected(idx) = True Then
sVal = sVal & csDelimeter & Me.lstDefects.ItemData(idx)
End If
Next idx
If Len(sVal) > Len(csDelimeter) Then
GetListValues = Mid(sVal, Len(csDelimeter) + 1)
End If
End Function
Private Sub SetItemsSelected(vArr As Variant)
Dim idx%, iVal%
' set selections from array
For iVal = 0 To UBound(vArr)
For idx = 0 To Me.lstDefects.ListCount - 1
If Me.lstDefects.ItemData(idx) = vArr(iVal) Then
Me.lstDefects.Selected(idx) = True
End If
Next idx
Next iVal
End Sub
Private Sub DoListboxStuff(Optional blnNoMsg As Boolean)
Dim sSQL$, sVal$, idx%, iVal%, vArr As Variant
If Me.lstDefects.ItemsSelected.Count = 0 Then
Me.lstDefects.RowSource = csDefaultRowSource
If blnNoMsg = False Then _
MsgBox "There are no selected items!", vbExclamation
Exit Sub
End If
For idx = 0 To Me.lstDefects.ListCount - 1
If Me.lstDefects.Selected(idx) = True Then
sVal = sVal & csDelimeter & Me.lstDefects.ItemData(idx)
End If
Next idx
sVal = GetListValues & ""
vArr = Split(sVal, csDelimeter) ' save selection in array
' new RowSource for lstDefects:
sSQL = "SELECT DefectID, Defect FROM" & vbCrLf & _
" (SELECT * FROM tbl_ProductDefects ORDER BY Defect) as Q01" & vbCrLf & _
" WHERE DefectID IN (" & sVal & ")" & vbCrLf & _
"UNION ALL" & vbCrLf & _
"SELECT DefectID, Defect FROM" & vbCrLf & _
" (SELECT * FROM tbl_ProductDefects ORDER BY Defect) as Q01" & vbCrLf & _
" WHERE DefectID NOT IN (" & sVal & ")"
'Debug.Print sSQL
Me.lstDefects.RowSource = sSQL
SetItemsSelected vArr
End Sub
Private Sub Form_Current()
Dim vArr As Variant
If Not IsNull(Me.SelectedIDs) Then
vArr = Split(Me.SelectedIDs, csDelimeter)
SetItemsSelected vArr
DoListboxStuff True 'Call function
End If
End Sub