Results 1 to 2 of 2
  1. #1
    Join Date
    May 2005
    Posts
    1,191

    Unanswered: Remove Selected Items from Value Listbox

    Hey all, I'm having a little trouble with some code. The basic idea is that a form has a textbox, two command buttons, and a value listbox. When the user types in a number to the textbox, they can then press one of the buttons to add that entry to the listbox; this works fine. Hopefully they can then select one or more items from the list, click the second button, and remove the selected items from the list; this I'm having trouble with. Here's what I got so far:
    Code:
        On Error Resume Next
        DoCmd.Hourglass True
    
        Dim ctl As Control, _
            frm As Form
        Dim i As Long
        
        Set frm = Forms!frmName
        Set ctl = frm!lstboxName
        
        ' ********
        ' Need to go backwards, from bottom up, so this method is
        ' insufficient
        ' ********
        ' Dim var As Variant
        ' For Each var In ctl.ItemsSelected
        '     ctl.RemoveItem (var)
        ' Next var
        ' ********
            
        For i = (ctl.ListCount - 1) To 0 Step -1
            If ctl.Selected(i) = True Then ctl.RemoveItem (i)
        Next i
        
        DoCmd.Hourglass False
        ctl.SetFocus
        Call subSetButtons
    When I go into debugger mode, I see that after it finds the first selected row and deletes it, it "resets" the selected rows; that is to say, it sets all of the rows to be unselected. So after the first selected row is deleted, it just runs through the rest of the For loop without deleting any of the other rows that were supposed to be deleted. Any ideas on how to make this work for more than one row?
    Me.Geek = True

  2. #2
    Join Date
    May 2005
    Posts
    1,191
    I got the following to work, using some functions in a public module borrowed from The Access Web
    Code:
        
        On Error Resume Next
        DoCmd.Hourglass True
        
        Dim ctl As Control
        Dim strDelete As String
        Dim var As Variant
        Dim i As Long
        
        Set ctl = Forms!frmName!lstboxName
        
        'build delete string
        strDelete = ""
        For Each var In ctl.ItemsSelected
            If Len(strDelete) > 0 Then strDelete = strDelete & ";"
            strDelete = strDelete & var
        Next var
        
        'parse and delete
        For i = CountCSWords(strDelete) To 1 Step -1
            ctl.RemoveItem (CLng(GetCSWord(strDelete, i)))
        Next i
        
        DoCmd.Hourglass False
        ctl.SetFocus
        Call SetButtons
    Unless someone has an easier way...
    Me.Geek = True

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •