Select all items from list box or just one

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • KMEscherich
    New Member
    • Jun 2007
    • 69

    #1

    Select all items from list box or just one

    Hi there, does anyone by chance know how to code a list box so the end-user can select either one item in a list box or all items in the list box???

    Not sure how to do this and really hope someone can assist me with this.

    Thank you VERY much for this.

    Example would be:

    Animal List Box options:
    Cat
    Dog
    Horse

    I would like the end-user to have the option of just selecting Cat or selecting all 3 animals.
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    #2
    The following Code will check and see if either 1 or ALL Items in a List Box named lstTest have been Selected. If any number of Items other that 1 or ALL of the Items have been selected, then a Message Box will alert the User, and the List Box will be cleared of Selections.
    Code:
    Dim lst As ListBox
    Dim intCtr As Integer
    
    Set lst = Me![lstTest]
    
    With lst
      'No Items selected
      If .ItemsSelected.Count = 0 Then Exit Sub
    
      'Check and see if either 1 or ALL Items are selected
      If .ItemsSelected.Count = 1 Or .ItemsSelected.Count = .ListCount Then
        'Do nothing, OK
      Else
        'Neither 1 nor ALL Items were Selected, so give the User a Message Box, then
        'Clear the List Box selections
        MsgBox "You can only select 1 or ALL Items in the List Box", vbExclamation, "Items selected"
          For intCtr = 0 To .ListCount - 1
            .Selected(intCtr) = False
          Next
      End If
    End With
    P.S. - The MultiSelect property of the List Box must be set to Extended.

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32669

      #3
      This is only available for ListBox controls (Not ComboBoxes), but the .MultiSelect property must be set to Simple or Extended.

      Comment

      Working...