[AccessD] Setting listbox selection

Charlotte Foust cfoust at infostatsystems.com
Thu Aug 16 13:05:21 CDT 2007


That won't give you what you want, as you've discovered.  Here's a
routine I used on a pick list form that built a string from the
selections:
'****** Begin Code
Private Function BuildListString(ByRef lst As ListBox, _
                                Optional blnSelectedOnly As Boolean =
False, _
                                Optional blnAll As Boolean = True) As
String
  'created 12/4/2001 CF
  'accepts a listbox control
  ' modified    7/2/2002  CF - Added blnAll argument and modified to
allow creation of
  '                            list of unselected items
  Dim strList As String 'holds return value of function
  Dim intCnt As Integer 'used for looping through entire list
  Dim varItem As Variant 'used for looping through selected items only
  
  On Error GoTo Proc_Err
  
  'loop through the listbox items and create a string
  
    If blnSelectedOnly Then
        'add only selected items to the list
        For Each varItem In lst.ItemsSelected
            strList = strList & "'" & lst.ItemData(varItem) & "', "
        Next varItem 'In lst.ItemsSelected
        If strList <> vbNullString Then
            strList = Left$(strList, Len(strList) - 2)
        End If 'strList <> vbNullString
    ElseIf blnAll Then
        For intCnt = 0 To lst.ListCount - 1
            'add all items to the list
            strList = strList & "'" & lst.ItemData(intCnt) & "'"
            If intCnt < lst.ListCount - 1 Then
                strList = strList & ", "
            End If 'intCnt < lst.ListCount - 1
        Next intCnt '= 0 To lst.ListCount - 1
    Else
        For intCnt = 0 To lst.ListCount - 1
            'only add unselected items to the list
            If Not lst.Selected(intCnt) Then
                strList = strList & "'" & lst.ItemData(intCnt) & "'"
                If intCnt < lst.ListCount - 1 Then
                    strList = strList & ", "
                End If 'intCnt < lst.ListCount - 1
            End If
        Next intCnt '= 0 To lst.ListCount - 1
    End If 'blnSelectedOnly
  
Proc_exit:
  On Error Resume Next
  BuildListString = strList
  Exit Function
Proc_Err:
  MsgBox "Error: " & Err.Description, vbExclamation, Me.Name & ":
BuildListString"
  Resume Proc_exit
End Function 'BuildListString(ByRef lst As ListBox) As String
'***** End Code

Charlotte Foust

-----Original Message-----
From: accessd-bounces at databaseadvisors.com
[mailto:accessd-bounces at databaseadvisors.com] On Behalf Of Christopher
Jeris
Sent: Thursday, August 16, 2007 10:24 AM
To: Access Developers discussion and problem solving
Subject: [AccessD] Setting listbox selection

-----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-----
--
AccessD mailing list
AccessD at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/accessd
Website: http://www.databaseadvisors.com




More information about the AccessD mailing list