Results 1 to 12 of 12
  1. #1
    Dave D is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Jun 2014
    Posts
    45

    Listbox.Selected not working

    I do not know why this isn't working. The "selected" is never realized. You will also notice it isn't even capitalized; it's like access doesn't recognize the property. The crazy thing is I have similar code in a double-click sub and that works fine ("selected still isn't capitalized, though).

    I use myVar to go back to the selected record after the listbox is requeried.

    Your help is greatly appreciated.

    Code:
    Private Sub lstPartsInSelectedSow_Click()
    
        Dim i As Integer
        
        For i = 1 To Me.lstPartsInSelectedSow.ListCount - 1
            If Me.lstPartsInSelectedSow.selected(i) Then
                myVar = i           'myVar is a global variable of type Variant
            End If
        Next I
    
    End Sub


  2. #2
    thebigthing313 is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2014
    Posts
    119
    The similar code On Dbl Click, is this on the same database? If not then maybe on this one you accidentally removed the reference that contains the Listbox object. You can check VBA Editor>Tools>References, see if the Microsoft Access Object Library isn't checked.

    Aside from that, I'm not entirely sure what else it could be. You can try a compact and repair.

  3. #3
    Dave D is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Jun 2014
    Posts
    45
    The similar double-click code is in the same db, on the same form for the same listbox. I DID wonder if something was corrupted so I imported everything into a new db - same problem.

    Strangely, this works if I set the listbox's multi-select property to 'simple' but I don't want multi-select, I only want single-select. Multi-select would screw everything up.

  4. #4
    thebigthing313 is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2014
    Posts
    119
    Regardless of the listbox's multi-select property, Listbox.Selected property should still work. So does the code work and it's just not autocapitalizing "Selected" to indicate that Access recognized the property, or the code doesn't work at all?

    And if the code doesn't run, what error are you getting? If Access is really not recognizing the Listbox's Selected property, you would get a compile error saying that the method or data member was not found. Is this the error you are getting?

  5. #5
    Dave D is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Jun 2014
    Posts
    45
    The code runs - I can step through each line and watch it. However, it doesn't recognize any row in the listbox as being selected. I even tried putting this code in the After_Update sub, but the same results.

    This code works, btw:

    Code:
    Private Sub lstPartsInSelectedSow_DblClick(Cancel As Integer)
        Dim i As Integer
        Dim pRcd As Long
        Dim sRcd As Long
        
        For i = 1 To Me.lstPartsInSelectedSow.ListCount - 1
            If Me.lstPartsInSelectedSow.selected(i) Then
                sRcd = Me.lstPartsInSelectedSow.Column(0, i)
                pRcd = Me.lstPartsInSelectedSow.Column(4, i)
                Debug.Print "sRcd = " & sRcd
                myVar = i
            End If
        Next i
        
        DoCmd.OpenForm "frmEditSowParts", , , "sowId = " & sRcd & " AND tblParts.pRecordId = " & CLng(pRcd)
    End Sub

  6. #6
    thebigthing313 is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2014
    Posts
    119
    Hrmm. Ok, so when select one (single-click), you want to set myVar to the value right? If nothing else, instead of iterating through the list items to find the selected value, would

    myVar = Me.lstPartsinSelectedSow.Value

    work instead?

    I would need to see the DB myself to troubleshoot further.

    EDIT: Nevermind, you were passing the counter i to myVar. Is there a reason you're passing the counter value, instead of the actual listbox value?

  7. #7
    Dave D is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Jun 2014
    Posts
    45
    This list box is a down-select from another list box and the value is a PK on a 1:M so it's all the same. To get other data from another column then I'd need to know which row in the list box is selected. It's a catch-22.

    Attached is a simple example of my problem. Open the form and single click and then double click. Only the double-click works.
    Attached Files Attached Files

  8. #8
    thebigthing313 is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2014
    Posts
    119
    Weird. It seems the Selected Property Array isn't updated until the control loses focus (though I'm not sure why dbl click works but not single click). If I made my selection in the listbox, then used a command button to return the selected row number, the selected-loop works. But not when the listbox goes through Click or AfterUpdate. It behaves as if nothing was still selected. I'm not sure I know what's happening here, maybe someone else can shed some light.

    However, you can try using:

    myVar = Me.lstPartsInSelectedSow.ListIndex

    to get the row number.

  9. #9
    Dave D is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Jun 2014
    Posts
    45
    .ListIndex worked swimmingly. Thank you!

    Excellent insight into the .Selected property. I, too, am interested if anyone else can identify why this property doesn't work if the object has focus.

    I'll leave the thread open for now and, if no one else comes back with an answer within a week or so, I'll set the thread as solved.

  10. #10
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,963
    Selected is capitalized by VBA editor when I opened the db, however, code does not work.

    This is not a multi-select listbox. Why bother with code that cycles through the list anyway? Just take the Value of listbox. I would use AfterUpdate event.

    For multi-select listbox, review http://allenbrowne.com/ser-50.html
    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.

  11. #11
    Dave D is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Jun 2014
    Posts
    45
    I see this, now.

    Do you know why the .Selected will not work for a single click?

  12. #12
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,963
    Nope. Never seen code that uses it. Doesn't seem to work with any event.
    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.

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

Similar Threads

  1. Replies: 9
    Last Post: 08-16-2013, 01:49 AM
  2. How to retain values selected in a listbox
    By Cedarguy in forum Forms
    Replies: 4
    Last Post: 05-09-2012, 10:05 AM
  3. Replies: 3
    Last Post: 02-13-2012, 08:14 AM
  4. Selected items in listbox
    By tomodachi in forum Access
    Replies: 1
    Last Post: 09-09-2010, 01:14 PM
  5. Exporting report selected from a listbox to excel
    By GARCHDEA in forum Import/Export Data
    Replies: 1
    Last Post: 08-10-2010, 07:45 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