• Listboxes – deselecting an item (Acc 97 SR-2)

    Home » Forums » AskWoody support » Productivity software by function » MS Access and database help » Listboxes – deselecting an item (Acc 97 SR-2)

    Author
    Topic
    #384059

    Does anyone know how to clear the selection in a Listbox?

    I am using a series of Listboxes from which a user can select criteria in order to filter the output of a report. Some of them are multi-select, others are not.

    I want to be able to clear all the selections in all the listboxes after the report has printed so the user has to make completely new selections for a subsequent report.

    The control only has 2 properties, neither of which I can leverage to achieve my result.

    I can’t believe I have to post this question. But I can’t afford to waste any more time on it.

    Looking forward to hearing how easy it is!

    Thanks,
    Rich P.
    (humbled by a 2-property control!)

    Viewing 1 reply thread
    Author
    Replies
    • #657325

      Have you tried requerying the listbox? How you deselect depends on the kind of listbox it is. Is it simple or multiselect?

    • #657338

      (Edited by HansV on 28-Feb-03 17:18. corrected error in loop index)

      Are you using a custom control? The standard list box that comes with Access has many more than 2 properties.

      To deselect all items in a standard Access list box, you can use

      Dim i As Integer
      For i = 0 To lbxSomething.ListCount – 1
      lbxSomething.Selected(i) = False
      Next i

      where lbxSomething is the name of the list box.

      • #657417

        First of all, thanks so much for the timely response. It is exaclty what I needed. Though I know I would not have found this anywhere else!
        For anyone interested, I used Hans’ solution with a slight modification. I have a few listboxes to clear, so I created a function that takes a Listbox control as an argument. Also, in order to clear the selection where the first item in the list is selected, I changed the counter in the loop to begin at ‘0’ instead of ‘1’.

        Private Function ResetList(lst As Access.ListBox)
        Dim i As Integer

        For i = 0 To lst.ListCount – 1
        lst.Selected(i) = False
        Next i

        End Function

        Then I can call this function from a command button placed next to a listbox control on the form:

        Private Sub cmdResetPgmMgr_Click()
        Call ResetList(Me.lstStaff)
        End Sub

        Thanks again for the answer. I should have asked sooner, rather than waste all that time.

        Regards,

        Rich P.

        • #657423

          Rich,

          You are correct in letting the loop index vary from 0 to ListCount – 1. The list index is zero-based. I have modified my previous reply.

    Viewing 1 reply thread
    Reply To: Listboxes – deselecting an item (Acc 97 SR-2)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: