Results 1 to 10 of 10
  1. #1
    Voodeux2014 is offline Competent Performer
    Windows 7 64bit Access 2013
    Join Date
    Nov 2014
    Location
    Slidell, LA
    Posts
    130

    Question ListBox selecting all rows from first row to selected row on click...

    Code:
    Public Sub lstAllStudentsData()
        Dim db As DAO.Database
        Dim rs As DAO.Recordset
        Dim strSQL As String, strItem As String
        
        Dim i As Integer
    
    
               For i = 1 To [Forms]!frmAddClassRoster.lstAllStudents.ListCount
    
    
                   'Remove an item from the ListBox.
                   [Forms]!frmAddClassRoster.lstAllStudents.RemoveItem 0
    
    
               Next i
     
       
       strSQL = "SELECT ID, FirstName, LastName, SSN FROM tbl1Students ORDER BY FirstName, LastName"
       Set db = CurrentDb
       Set rs = db.OpenRecordset(strSQL)
       Do Until rs.EOF
          strItem = rs.Fields("ID").Value & ";" _
             & rs.Fields("FirstName").Value _
             & " " _
             & rs.Fields("LastName").Value _
             & " (" _
             & rs.Fields("SSN").Value _
             & ")"
          [Forms]!frmAddClassRoster.lstAllStudents.AddItem strItem      ' Row Source Type must be Value List
          rs.MoveNext
    
    
       Loop
       
      rs.Close
       Set rs = Nothing
       Set db = Nothing
    End Sub
    That is the code for the data that gets entered into the listbox.
    The listbox is set to extended
    The listbox row source type is value list

    The listbox is located on a form that has two listboxes:

    lstAllStudents and lstSelectedStudents



    This allows for movement between the two listboxes.

    When selecting a row in lstAllStudents it highlights that selection and everything above it to the very first item in the list.
    However, when clicking the button to move the selected items to the other listbox, it only moves the row that was last clicked, even if holding CTRL to have multiple rows selected.

    What is the best way to work around this issue?

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Where is code that builds the lstSelectedStudents list?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Voodeux2014 is offline Competent Performer
    Windows 7 64bit Access 2013
    Join Date
    Nov 2014
    Location
    Slidell, LA
    Posts
    130
    The list is empty until items are moved over via cmd buttons:
    Code:
    Private Sub cmdMoveAllToList1_Click()
        MoveAllItems "lstSelectedStudents", "lstAllStudents"
    End Sub
    
    Private Sub cmdMoveToList1_Click()
        MoveSingleItem "lstSelectedStudents", "lstAllStudents"
    End Sub
    
    Private Sub cmdMoveToList2_Click()
         MoveSingleItem "lstAllStudents", "lstSelectedStudents"
    End Sub
    
    Private Sub cmdMoveAllToList2_Click()
        MoveAllItems "lstAllStudents", "lstSelectedStudents"
    End Sub
    
    Sub MoveSingleItem(strSourceControl As String, strTargetControl As String)
        
        Dim strItem As String
        Dim intColumnCount As Integer
        For intColumnCount = 0 To Me.Controls(strSourceControl).ColumnCount - 1
            strItem = strItem & Me.Controls(strSourceControl).Column(intColumnCount) & ";"
        Next
        strItem = Left(strItem, Len(strItem) - 1)
    
        'Check the length to make sure something is selected
        If Len(strItem) > 0 Then
            Me.Controls(strTargetControl).AddItem strItem
            Me.Controls(strSourceControl).RemoveItem Me.Controls(strSourceControl).ListIndex
        Else
            MsgBox "Please select an item to move."
        End If
    
    End Sub
    
    Sub MoveAllItems(strSourceControl As String, strTargetControl As String)
        Dim strItem As String
        Dim intColumnCount As Integer
        Dim lngRowCount As Long
        
        For lngRowCount = 0 To Me.Controls(strSourceControl).ListCount - 1
            For intColumnCount = 0 To Me.Controls(strSourceControl).ColumnCount - 1
                strItem = strItem & Me.Controls(strSourceControl).Column(intColumnCount, lngRowCount) & ";"
            Next
            strItem = Left(strItem, Len(strItem) - 1)
            Me.Controls(strTargetControl).AddItem strItem
            strItem = ""
        Next
            
        Me.Controls(strSourceControl).RowSource = ""
    End Sub
    That is all the code that is associated with the two listboxes before adding the data to tables.

    The problem is that it is highlighting everything from what is selected up to the first entry in the listbox, but only moving the item that was clicked.

    I would like it to be where i can use the extended property to use CTRL to select multiple students to move to the other listbox.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Step debug. Set breakpoint. What is the ListCount property value? How many times is the loop iterated - just one? It would appear that even though highlighted, items are not actually selected.

    I don't have any db with this setup. If you want to provide yours for analysis and testing, follow instructions at bottom of my post.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    Voodeux2014 is offline Competent Performer
    Windows 7 64bit Access 2013
    Join Date
    Nov 2014
    Location
    Slidell, LA
    Posts
    130
    I am not sure what you mean by the ListCount property. I do not see it in the properties area. It only goes through the loop once. Here is my database.

    In order for the form to open properly, you must select a grant in the combobox on the main form (only one available at the moment), then select one of the training providers in the listbox that appears. Finally click "input roster" to get to the form in question. (frmAddClassRoster)
    Attached Files Attached Files
    Last edited by Voodeux2014; 11-18-2015 at 07:39 AM. Reason: providing instructions

  6. #6
    Voodeux2014 is offline Competent Performer
    Windows 7 64bit Access 2013
    Join Date
    Nov 2014
    Location
    Slidell, LA
    Posts
    130
    The highlight issue no longer occurs. But even when selecting multiple rows, it only adds the last row.

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    I cannot review db until Nov 28. If you still need help then, bump this thread with another post.


    You use ListCount in your posted code.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  8. #8
    Voodeux2014 is offline Competent Performer
    Windows 7 64bit Access 2013
    Join Date
    Nov 2014
    Location
    Slidell, LA
    Posts
    130
    ListCount is only used when getting the data for lstAllStudents or when moving all rows over to lstSelectedStudents. It is not used when not moving all rows between listboxes.

  9. #9
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Sorry, ListCount maybe wrong property. My suggestion is to somehow determine how many items have been selected as a debugging tool. Unfortunately, not finding a way to do that wtihout actually looping through the entire list.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  10. #10
    Perceptus's Avatar
    Perceptus is offline Expert
    Windows 7 64bit Access 2007
    Join Date
    Nov 2012
    Location
    Knoxville, Tennessee
    Posts
    659

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Double Click Listbox
    By HelpDesk in forum Access
    Replies: 12
    Last Post: 06-29-2015, 10:38 AM
  2. Selecting rows from Datasheet View
    By MatthewGrace in forum Programming
    Replies: 1
    Last Post: 10-30-2014, 12:15 AM
  3. Replies: 2
    Last Post: 02-01-2013, 05:23 PM
  4. Replies: 3
    Last Post: 05-15-2012, 02:51 PM
  5. Replies: 2
    Last Post: 12-31-2011, 07:03 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums