Results 1 to 14 of 14
  1. #1
    bubai is offline Competent Performer
    Windows 7 32bit Access 2016
    Join Date
    Nov 2014
    Location
    Dhaka
    Posts
    162

    Search within access listbox rowsource

    I have placed an Unbound listbox on my bound form. The listbox's rowsource is an SQL query. Is it possible to Search within the listbox rowsource and find the ordinal position of a row?
    There is a common field between my form's RecordSource and the listbox's rowsource which is "Pay_ID". So whenever my form moves to a record I would like to search the listbox's rowsource by the form's "Pay_ID" control value and get the ordinal position and select the listbox using the Me.lstBankPay.Selected(x) = True formula. I have tried the following method but it does not seem to give accurate result.



    Code:
    'Dim rsR As Recordset
    'Set rsR = Me.lstBankPay.Recordset
    'On Error Resume Next
    'rsR.FindFirst "Pay_ID = " & Me.Pay_ID
    'If Not rsR.NoMatch Then
    'Me.lstBankPay.Selected(rsR.AbsolutePosition - 1) = True
    'End If
    'rsR.Close
    'Set rsR = Nothing
    Last edited by bubai; 01-23-2022 at 04:10 AM.

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,898
    Listbox is bound to Pay_ID field? A bound single-select lisbox will already put focus on item saved into field.

    I don't really understand what you want to accomplish with this 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.

  3. #3
    bubai is offline Competent Performer
    Windows 7 32bit Access 2016
    Join Date
    Nov 2014
    Location
    Dhaka
    Posts
    162
    Quote Originally Posted by June7 View Post
    Listbox is bound to Pay_ID field? A bound single-select lisbox will already put focus on item saved into field.
    ListBox is Unbound.
    But its Rowsource query has a field named "Pay_ID" and in my bound form I also have a control named "Pay_ID". I want to search the listbox by this field.

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,911
    I believe you would need to cycle through the listbox and set the relevant row as selected if found?

    Most times, the process is the other way around?, you select something in the list/combo and a record is found, not the other way around?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  5. #5
    bubai is offline Competent Performer
    Windows 7 32bit Access 2016
    Join Date
    Nov 2014
    Location
    Dhaka
    Posts
    162
    Quote Originally Posted by Welshgasman View Post
    I believe you would need to cycle through the listbox and set the relevant row as selected if found?
    Could you help me with the code?
    I guess I have to put the code to the OnCurrent event of the form.

  6. #6
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,911
    Quote Originally Posted by bubai View Post
    Could you help me with the code?
    I guess I have to put the code to the OnCurrent event of the form.
    Why not use Google? That is always my first point of call. Someone has generally wanted to do it before me.

    From https://www.google.com/search?q=set+...hrome&ie=UTF-8

    you will get

    https://stackoverflow.com/questions/...-list-box-item

    Yes, current would be a good place.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,898
    Is this on a Single View form?
    Code:
    Dim i As Integer
    With Me.lstBankPay
        For i = 0 To .ListCount - 1
            If .Column(0, i) = Me.Pay_ID Then
                .Selected(i) = True
                Exit For
            End If
        Next
    End With
    Did you step debug your code? I did and it works when I remove the - 1. AbsolutePosition is zero-based.
    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
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,651
    I think what your looking for is - under the forms OnCurrent event is

    Code:
    Me.lstBankPay = nz(Me.Pay_ID,0)
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  9. #9
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,898
    Yep, as simple as that, doh!

    Don't need Nz().
    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
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,651
    Quote Originally Posted by June7 View Post
    Yep, as simple as that, doh!

    Don't need Nz().
    Depends whether there is any other code dependant on the listbox not being null, like when moving to a new record.
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  11. #11
    bubai is offline Competent Performer
    Windows 7 32bit Access 2016
    Join Date
    Nov 2014
    Location
    Dhaka
    Posts
    162
    Quote Originally Posted by moke123 View Post
    I think what your looking for is - under the forms OnCurrent event is

    Code:
    Me.lstBankPay = nz(Me.Pay_ID,0)
    Wow!! It's working like charm.
    But how come it is selecting the listbox without a .Selected property?

  12. #12
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,898
    It's not selecting the listbox, just setting value of listbox.

    .Selected just sets a listbox item but doesn't put focus on the control.

    If you want to set focus on listbox, use: Me.lstBankPay.SetFocus
    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.

  13. #13
    bubai is offline Competent Performer
    Windows 7 32bit Access 2016
    Join Date
    Nov 2014
    Location
    Dhaka
    Posts
    162
    Quote Originally Posted by June7 View Post
    It's not selecting the listbox, just setting value of listbox.

    .Selected just sets a listbox item but doesn't put focus on the control.

    If you want to set focus on listbox, use: Me.lstBankPay.SetFocus
    Many thanks for your help.

  14. #14
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,651
    But how come it is selecting the listbox without a .Selected property?
    I believe the .selected property is primarily used in multiselect listboxes and the ItemsSelected collection.

    for instance the code below iterates through the ItemsSelected collection and sets them to false(unselected)
    You would use this to clear a multiselect listbox.

    Code:
            For Each varItem In lst.ItemsSelected
                lst.Selected(varItem) = False
            Next
    This iterates through the listbox items and selects all the items in a multiselect listbox.
    Code:
            For lngRow = 0 To lst.ListCount - 1
                lst.Selected(lngRow) = True
            Next
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

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

Similar Threads

  1. Replies: 15
    Last Post: 01-23-2019, 11:44 AM
  2. Replies: 2
    Last Post: 11-01-2018, 07:31 AM
  3. Reversing rowsource order for a multicolumn listbox
    By wackywoo105 in forum Programming
    Replies: 6
    Last Post: 12-10-2014, 06:26 AM
  4. Updating listbox rowsource not working
    By j2curtis64 in forum Forms
    Replies: 13
    Last Post: 12-07-2011, 01:46 PM
  5. ListBox RowSource Update Not working in 2003
    By Access0307 in forum Programming
    Replies: 1
    Last Post: 05-05-2011, 07:41 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