Public Function ProcessListBoxClick(ctlCaller As Control, _
Optional varBackColor_All As Variant = 16777215, _
Optional varForeColor_All As Variant = 0)
'********************************
'* Declaration Specifications *
'********************************
Dim frmListBox As Form 'Active form
Dim ctlActive As Control
Dim lngForeColor_Off As Long 'Color of the label when "All" is NOT selected
Dim lngForeColor_On As Long 'Foreground color of the label when "All" IS selected.
Dim lngBackColor_Off As Long 'Background color of the label when "All" is NOT selected
Dim lngBackColor_On As Long 'Background color of the label when "All" IS selected
Dim lngColor As Long 'Working variable
Dim i As Long 'Working variable
Dim strName_LB As String 'Name of list box control
Dim strName_All As String 'Name of label control appended with "_All"
Dim strName_Total As String 'Name of label control that shows totals
Dim varItem As Variant 'Working variable
'****************
'* Initialize *
'****************
On Error GoTo ErrHandler
Set frmListBox = Screen.ActiveForm
Set ctlActive = ctlCaller
'*******************************************
'* Determine the names of the 3 controls *
'*******************************************
If (InStr(ctlActive.Name, "_All") > 0) Then 'IFT, User clicked on "_All" label control
strName_LB = "lst" & Mid(ctlActive.Name, 4, Len(ctlActive.Name) - 7) '"lstName"
strName_All = ctlActive.Name '"lblName_All"
strName_Total = "lbl" & Mid(strName_LB, 4) '"lblName"
Else 'IFT, User clicked on list box
strName_LB = ctlActive.Name '"lstName"
strName_All = "lbl" & Mid(strName_LB, 4) & "_All" '"lblName_All"
strName_Total = "lbl" & Mid(strName_LB, 4) '"lblName"
End If
'********************************************************************
'* Determine the color of the "All" label when it is NOT selected *
'********************************************************************
lngForeColor_Off = ctlActive.ForeColor
lngBackColor_Off = ctlActive.BackColor
'****************************************************************
'* Determine the color of the "All" label when it IS selected *
'****************************************************************
lngBackColor_On = varBackColor_All
lngForeColor_On = varForeColor_All
'**********************************************************
'* If the "All" label was selected, then: *
'* 1. Highlight the "All" label *
'* 2. Deselect all of items selected in the list box *
'**********************************************************
If ((strName_All = ctlActive.Name) Or (frmListBox(strName_LB).ItemsSelected.Count = 0)) Then
frmListBox(strName_All).ForeColor = lngForeColor_On
frmListBox(strName_All).BackColor = lngBackColor_On
For Each varItem In frmListBox(strName_LB).ItemsSelected 'Deselect list box items
frmListBox(strName_LB).selected(varItem) = False
Next varItem
Else
frmListBox(strName_All).ForeColor = lngForeColor_Off
frmListBox(strName_All).BackColor = lngBackColor_Off
End If
'**********************************************************
'* Display the number of items selected in the list box *
'**********************************************************
If (frmListBox(strName_LB).ItemsSelected.Count = 1) Then
frmListBox(strName_Total).Caption = "(" & frmListBox(strName_LB).ItemsSelected.Count & " item selected)"
Else
frmListBox(strName_Total).Caption = "(" & frmListBox(strName_LB).ItemsSelected.Count & " items selected)"
End If
'***********************
'* Exit Subprocedure *
'***********************
ExitProcedure:
Exit Function
'****************************
'* Error Recovery Section *
'****************************
ErrHandler:
If (Err.number = 2164) Then Resume Next
MsgBox Err.Description, vbExclamation
Resume ExitProcedure
End Function