Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Code needed to detect if at least one of the item is selected.

Status
Not open for further replies.

maupiti

Programmer
Oct 27, 2003
240
US
I have a list box, and its Muti-Select property is set to Extended. What is the VBA code use to detect if at least one of the item is select from the list ?

If I use the code below to check whether the value of strSQL is null or not, then it would not work if a user, unselect the last item in the list. When the last item in the list is unselected, the event AfterUpdate never gets activated. Since the AfterUpdate events was not activated, then the value strSQL will always contains,the last item that was click (it will never contains a Null value)/

///////////////////////////////////////////////////

Private Sub List_Bus_AfterUpdate()

Dim varItem As Variant
Dim strSQL As String
Dim strSQL_No_Comma_At_End As String

For Each varItem In Me!List_Bus.ItemsSelected
strSQL = strSQL & Me!List_Bus.ItemData(varItem) & ", "
Next varItem

strSQL_No_Comma_At_End = Left$(strSQL, Len(strSQL) - 2)
End Sub
 
I got it figure out.

/////////////////////////////////////////

Private Sub Bus_Wire_List_Click()
Dim stDocName_2 As String
Dim varItem As Variant
Dim At_Least_One_Was_Selected As Boolean

At_Least_One_Was_Selected = False

For Each varItem In Me!List_Bus.ItemsSelected
If Me!List_Bus.Selected(varItem) = True Then
At_Least_One_Was_Selected = True
stDocName_2 = "Bus_Wire_List_2"
DoCmd.OpenReport stDocName_2, acPreview
End If
Next varItem

If At_Least_One_Was_Selected = False Then
MsgBox "Please select one or more items from the above list box.", vbExclamation
End If
End Sub
 
maupiti,

I think you could also use the .Count property of the ItemsSelected collection:
Code:
If Me!List_Bus.ItemsSelected.Count > 0 Then...
HTH,

Ken S.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top