[AccessD] Setting listbox selection

Christopher Jeris cjeris at fas.harvard.edu
Thu Aug 16 12:24:23 CDT 2007


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi everyone,

Some behavior of the .Selected attribute of the ListBox control is
baffling me.  Specifically, setting the .Selected attribute of a
single-select listbox element, when that element is not in the currently
viewable region of a scrollable listbox, does not appear to work.

Here's a test case on Access 2002 SP3.

Create a form containing a single listbox List0, with
  Row Source Type = Value List
  Row Source = 0;1;2;3;4;5;6;7;8;9
Size the listbox so that not all the rows are visible (there is a scroll
bar).

and a single command button Command2, with the following code:

Private Sub EraseSelection(box As ListBox)
    Dim i As Long
    For i = 0 To box.ListCount - 1
        box.Selected(i) = False
    Next i
End Sub

Private Sub Command2_Click()
    EraseSelection Me!List0
    If Me!List0.ItemsSelected.Count > 0 Then
        MsgBox Me!List0.ItemsSelected(0)
    End If
End Sub

To test:
Select an element in the listbox.  Click Command2.  The selection
disappears.

Select an element in the listbox.  Scroll the listbox so that the
highlighted row is ENTIRELY beyond the viewable region (the problem does
not occur if the highlight is partially visible).  Click Command2.  I
get two MsgBoxes; the first one gives the highlighted row; the second
one gives -1.

In other words, setting the selected item of a listbox using the
.Selected property may silently fail, depending entirely on a fact (the
view region of the listbox) which I know no way to find out in code!

What's going on here, and what should I be doing instead?

thanks much, Chris Jeris

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGxIhG5ICCNV0oGWARArGOAJ0eBHDrdgKvHbAItVYyWPdkfEQ49gCfX+N7
KFOnbvPT9yFOZKAOunudvWU=
=E+V2
-----END PGP SIGNATURE-----



More information about the AccessD mailing list