Remove selected items from List Box in Microsoft Access

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • errol999
    New Member
    • Apr 2010
    • 7

    Remove selected items from List Box in Microsoft Access

    I'm using Access 2003.

    I have a List Box which I populate by selecting items from a drop down Combo Box. I now want to delete from the List Box only the selected, but not necessarily sequencial items (either one at a time or several).

    I found some code at this website:
    In this tutorial, you will learn how to add and remove an item from a Listbox in Access 2000 without the AddItem and RemoveItem methods.


    The author suggests creating a Text Box, then first temporarily storing the text that is selected in the List Box as follows:

    Code:
    MyText = MyList.ItemData(MyList.ListIndex)
    Then, by use of the following code, the text present in the Text Box is used to search the List Box, and the corresponding text is deleted:

    Code:
    Dim S As String
    S = MyText & ";"
    MyList.RowSource = Replace(MyList.RowSource, S, "")
    The first bit works fine, i.e. the text selected in the List Box appears in the Text Box, but I have no luck when I choose to delete this text from the List Box.

    My understanding is that this method would also only work for one line of text, and not multiple selections.

    Any suggestions would be helpful.
  • NeoPa
    Recognized Expert Moderator MVP
    • Oct 2006
    • 32633

    #2
    This approach will only work if the ListBox is populated manually. If RowSource is a query then this will not work.

    Multiple selections can be made to work, but I suggest you get the fundamentals working correctly first.

    Welcome to Bytes!

    Comment

    • ADezii
      Recognized Expert Expert
      • Apr 2006
      • 8834

      #3
      If the Row Source of your List Box is a Value List, you can use the following code which does not require the use of a Text Box:
      Code:
      Dim lst As ListBox
      Dim intRowCtr As Integer
      Dim strBuild As String
      
      'Substitute your own List Box Name
      Set lst = Me![lstTest]
      
      With lst
        If .ItemsSelected.Count = 0 Then Exit Sub
      
        For intRowCtr = 0 To .ListCount - 1
          If Not .Selected(intRowCtr) Then
            strBuild = strBuild & .ItemData(intRowCtr) & ";"
          End If
        Next
      
        strBuild = Left$(strBuild, Len(strBuild) - 1)
      
        .RowSource = strBuild
      End With

      Comment

      • errol999
        New Member
        • Apr 2010
        • 7

        #4
        Originally posted by NeoPa
        This approach will only work if the ListBox is populated manually. If RowSource is a query then this will not work.

        Multiple selections can be made to work, but I suggest you get the fundamentals working correctly first.

        Welcome to Bytes!
        Thanks. The Listbox is populated manually - I'll work on the fundamentals first.

        Comment

        • errol999
          New Member
          • Apr 2010
          • 7

          #5
          Originally posted by ADezii
          If the Row Source of your List Box is a Value List, you can use the following code which does not require the use of a Text Box:
          Code:
          Dim lst As ListBox
          Dim intRowCtr As Integer
          Dim strBuild As String
          
          'Substitute your own List Box Name
          Set lst = Me![lstTest]
          
          With lst
            If .ItemsSelected.Count = 0 Then Exit Sub
          
            For intRowCtr = 0 To .ListCount - 1
              If Not .Selected(intRowCtr) Then
                strBuild = strBuild & .ItemData(intRowCtr) & ";"
              End If
            Next
          
            strBuild = Left$(strBuild, Len(strBuild) - 1)
          
            .RowSource = strBuild
          End With
          Thanks ADezii. I'll give that a try.

          Comment

          • errol999
            New Member
            • Apr 2010
            • 7

            #6
            Originally posted by errol999
            Thanks ADezii. I'll give that a try.
            Ok, this worked perfectly, until I had just one item left if in the List Box. If I try and delete the last item I get run-time error 5 for this line:

            Code:
               strBuild = Left$(strBuild, Len(strBuild) - 1)
            which makes sense I think . I've not figured out yet how to get round this but might try putting in an If Then line to deal with this last item. If you have a suggestion that would be great. Thanks

            Comment

            • NeoPa
              Recognized Expert Moderator MVP
              • Oct 2006
              • 32633

              #7
              I would replace lines #12 through #19 with :
              Code:
                  If Not .Selected(intRowCtr) Then
                    strBuild = strBuild & ";" & .ItemData(intRowCtr)
                  End If
                Next intRowCtr
              
                If strBuild = "" Then strBuild = ";"
              
                .RowSource = Mid(strBuild, 2)

              Comment

              • errol999
                New Member
                • Apr 2010
                • 7

                #8
                Ok. That did the trick - thanks so much!

                Comment

                • NeoPa
                  Recognized Expert Moderator MVP
                  • Oct 2006
                  • 32633

                  #9
                  Pleased to hear it Errol :)

                  Basically, each element was attached to a separator character when found. If none was found, then the code that stripped out the separator character would fail as it was expecting one, but we had none there. Adding that to the situation where nothing was found allowed the code to work and pick up the empty string. All fine & dandy.

                  Comment

                  • errol999
                    New Member
                    • Apr 2010
                    • 7

                    #10
                    Originally posted by NeoPa
                    Pleased to hear it Errol :)

                    Basically, each element was attached to a separator character when found. If none was found, then the code that stripped out the separator character would fail as it was expecting one, but we had none there. Adding that to the situation where nothing was found allowed the code to work and pick up the empty string. All fine & dandy.
                    Thanks for the explanation which has helped me make more sense of it. I can see I still have a lot to learn, but have picked up some really useful tips on this question. I'll now persevere with getting the rest of the dbase functional!

                    Comment

                    • NeoPa
                      Recognized Expert Moderator MVP
                      • Oct 2006
                      • 32633

                      #11
                      That's music to my ears. So glad we could help :)

                      Comment

                      Working...