Share via

Access 2013 listbox bug

Anonymous
2017-01-22T18:28:12+00:00

I have created a Listbox  that has the Multiselect property set to 'Extended Multiselect'.

I am using VBA code to extract values from the items selected:

e.g.

for each varItm in Me!*listboxname.*selecteditems

variablename =  Me!*listboxname.*column(columnindex, varItm)

next varItm

The user selects a range of rows using click then shift click and then uses ctrl click to deselect a row or rows within the selection.  The selected values are returned ok but only the first contiguous block of selected rows remain selected.  This leaves the user in doubt as to whether all the selections have been processed.  Works fine for a range of rows or random set of rows using ctrl click but not the scenario described above.

Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

6 answers

Sort by: Most helpful
  1. Anonymous
    2017-02-08T13:04:19+00:00

    The OP has posted a solution to this in another thread, putting the following code in the control's LostFocus event procedure:

    Dim varItm As Variant

    With ListBoxNameGoesHere

    For Each varItm In .ItemsSelected

    .Selected(CLng(varItm)) = False

    .Selected(CLng(varItm)) = True

    Next varItm

    End With

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2017-01-23T18:01:49+00:00

    I see, Ken.  I didn't understand exactly what was going on.  I've tested and can confirm both your observation and that of the OP.  It doesn't seem to have anything to do with the code, actually; in my tests, as soon as the focus leaves the control, whether to go to another control or even if you click on the record selector, the combo box shows the incorrect selection, even though the rows are all still selected as far as VBA can tell.

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2017-01-23T17:09:48+00:00

    I don't think it's anything specific in the code, Dirk.  The problem seems to be a fundamental one when a multi-select list box's MultiSelect property is set to 'Extended' rather than "Simple'.  I can observe it in my Multiselect demo if I change the property to 'Extended' from its usual setting of 'Simple'.  It can rightly be called a bug, I think.  The code behind one of the forms in my demo is:

    Private Sub cmdOpenReport_Click()

        Dim varItem As Variant

        Dim strEmployeeIDList As String

        Dim strCriteria As String

        Dim ctrl As Control

        Set ctrl = Me.lstEmployees

        If ctrl.ItemsSelected.Count > 0 Then

            For Each varItem In ctrl.ItemsSelected

                strEmployeeIDList = strEmployeeIDList & "," & ctrl.ItemData(varItem)

            Next varItem

            ' remove leading comma

            strEmployeeIDList = Mid(strEmployeeIDList, 2)

            strCriteria = "EmployeeID In(" & strEmployeeIDList & ")"

            DoCmd.OpenReport "rptEmployees", _

                View:=acViewPreview, _

                WhereCondition:=strCriteria

        Else

            MsgBox "No employees selected", vbInformation, "Warning"

        End If

    End Sub

    If rows are selected en bloc and then selectively deselected with Ctrl-Click, after the above code has executed, only the topmost contiguous block of items in the list box remains selected.  As the OP says this could be confusing, but also it prevents the same set of selected rows being used again without reselecting the missing items.  My demo outputs the report in print preview, but if I had a second button to send the report to a printer for instance, the user would have to go to the trouble of making the further selections.

    The easy solution is to set the MultiSelect property to 'Simple', but with a long list this could make multiple selections tedious.  A possible solution with a seeing of 'Extended' might be to write each selected value to an array in the loop.  Then, after exiting the loop, reselect the items by stepping through the value in the array, setting the Selected property of the row to True for each.  I haven't tried it, though.

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2017-01-23T16:31:02+00:00

    Could we see your code so we can try to reproduce the problem?

    Was this answer helpful?

    0 comments No comments
  5. Anonymous
    2017-01-23T07:41:01+00:00

    Not exactly sure how you are using the ListBox, so...  Have a look here below, there are some examples that might help...

    http://www.baldyweb.com/

    Was this answer helpful?

    0 comments No comments