Results 1 to 4 of 4
  1. #1
    MykelDL is offline Novice
    Windows 10 Access 2013 64bit
    Join Date
    May 2017
    Posts
    22

    Selecting a record from an unbound listbox using a navigation button

    Hello.

    I can probably find a workaround for my particular problem by updating the record source for the form instead of just the listbox, but I'm betting there's a simple solution that I'm just not experienced enough to find.

    1. I've got an unbound listbox on a form that I use to direct-select navigate to records on the form.
    2. I also have form action buttons to control movement (next, previous, new, etc.)
    3. I have filters on the form that affect the records displayed in the listbox.


    If I filter the unbound listbox, the navigation buttons naturally don't go from one filtered record to the next.

    I adapted this code that I found (https://stackoverflow.com/questions/...ox-by-vba-code) for how to move to the NEXT record in a listbox by clicking the button, and the code almost works. It visually move the cursor to the NEXT record.

    Code:
    Private Sub cmdListBoxNext_Click
    With me.lstItem if .ItemSelected.Count = 0 Then Exit Sub 'Can't move if nothing is selected Dim lngCurrentPosition As Long lngCurrentPosition = .ItemSelected(0) If lngCurrentPosition = .ListCount then Exit Sub 'Can't move further than the last item Debug.Print me.lstItem .Selected(lngCurrentPosition) = False 'Deselect current item .Selected(lngCurrentPosition +1) = True 'Select next item
    Debug.Print me.lstItem
    End With
    I then tried to execute the lstItem_AfterUpdate code to update the form.

    What's happening is that even with moving the cursor in the listbox (the form visually updates), the value of me.lstItem remains the same, as shown by the before and after debug.print outputs. In fact, no matter how many lines down the cursor moves, the value of lstItem stays constant.

    I'm guessing there's a single piece of code I'm missing to actually update the control/Select the value of the line now represented in the listbox, I just cannot figure out what that is.



    Thanks in advance for your help.

  2. #2
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,122
    You may want to read this:
    https://learn.microsoft.com/en-us/of...stbox.itemdata
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  3. #3
    MykelDL is offline Novice
    Windows 10 Access 2013 64bit
    Join Date
    May 2017
    Posts
    22
    Thank you, Vlad. That's what I was looking for.

    I tweaked and compressed my code, so for those interested, here is where I ended up:

    Code:
    Private Sub cmdListBoxNext_Click
    
    With Me.lstItem
    If .ItemSelected.Count = 0 Then Exit Sub 'Can't move if nothing is selected If .ItemSelected(0) + 1 = .ListCount then Exit Sub 'Can't move further than the last item and have to account for 0-base index of the listbox
    Me.Recordset.FindFirst "[ID] = .ItemData(.ItemSelected(0) + 1) 'Go to the next record in the listbox's recordset
    End With

  4. #4
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,122

    Glad to be able to help!

    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

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

Similar Threads

  1. reference the correct button when selecting a record
    By rwahdan@gmail.com in forum Access
    Replies: 3
    Last Post: 02-05-2018, 01:19 PM
  2. Replies: 1
    Last Post: 06-23-2014, 05:25 AM
  3. Replies: 2
    Last Post: 02-01-2013, 05:23 PM
  4. Replies: 2
    Last Post: 12-21-2012, 01:57 PM
  5. Replies: 22
    Last Post: 06-12-2012, 10:02 PM

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