How do I clear the selection from a listbox?

shay

Registered User.
Local time
Today, 08:08
Joined
Apr 29, 2002
Messages
169
Hi

I'm using a listbox with the multiselect property set to 1 (simple multiple selection).

How do I reset the listbox ie clear the selection?
Can't find anything in the help text.

TIA

shay :cool:
 
Code:
    Dim varItem As Variant
    
    For Each varItem In Me.lstMyListBox.ItemsSelected
        Me.lstMyListBox.Selected(varItem) = False
    Next varItem
 
Worked a treat. Thanks mate!

shay :cool:
 
I've tried this, and it partly works, but now when I try to select a new Item in that list, it won't select.
 
Do you have an example of what you've done as what I posted does work.
 
What I have is an Option Group, and when the user clicks an option, it triggers a after update event.

The after update event creates a sql statement and puts it as the row source of my listbox.

My problem was this: If I select an item in the listbox, it selects it. Then if I chose another item in the option group it refreshed my list box. Then if I go back to the option I had previously, (the list is basically the same as when I selected an item, but with the rowsource requeried) the item I had selected is still selected.

After putting your code in the on_change even of the option group, it works, I can select an item in the listbox, change option, I see the list refresh, change back to previous option, and the item is not selected, which is what I want, but I can't select an item anymore. The listbox just flickers if I try to select something.

Here is the relevent code:
Code:
Private Sub optSystem_AfterUpdate()
    
    requeryCiuList
    
    Dim varItem As Variant
    
    For Each varItem In Me.lstCIUs.ItemsSelected
        Me.lstCIUs.Selected(varItem) = False
    Next varItem
    
End Sub

Private Sub requeryCiuList()
    
    Select Case optSystem
    Case 1
        sqlSystem = "tmiu"
    Case 2
        sqlSystem = "cmiu"
    End Select
    
    lstCIUs.RowSource = "SELECT ciuId, ciuName FROM CIUs WHERE ciuSystem = '" + sqlSystem + "' order by ciuName;"
    
End Sub

Private Sub lstCIUs_AfterUpdate()
    'Modifies another listbox that works fine
End Sub

Hope somebody can figure out the problem... I even tried deleting the listbox and recreating another one, but the same problem persisted. I've done similar, but not exactly, the same thing on other forms with no problems...

Thanks for taking a look at my question Mile-o-Phile
 
Why are you calling the requeryCiuList sub just before you clear the selection in the list box?

Here is how I clear all selections in a list box named "lbAssociateList" ...
Code:
Private Sub bDeselectAll_Click()

    Dim i As Integer
    Dim intListCount As Integer
    
    intListCount = lbAssociateList.ListCount - 1
    
    For i = 0 To intListCount
    lbAssociateList.Selected(i) = False
    Next

End Sub
 
ghudson said:
Why are you calling the requeryCiuList sub just before you clear the selection in the list box?

Because if I put it after, the item stays selected. I've tried your code too, but same result.

I only mentionned earlier what is relevent code, but I actually have 2 option groups and 2 list boxes on my form. One list box depends on the 2 options groups, and the second listbox depends on the selection in the first listbox.

I've been working on this form for a while, and I think starting over may be more productive. I have managed to complete a form which does almost exactly what this one should, so... you know... with Access, you know... Microsoft...
 
Created new form... same problem

Noticed something new however, I was so focused on the listbox I didn't notice before that when I can't select a new item in the listbox, I also cannot change the options in one of the option groups. The option group used to change the listbox still works, it's the other one that doesn't

This very is strange...
 

Users who are viewing this thread

Back
Top Bottom