Results 1 to 7 of 7
  1. #1
    MadTom's Avatar
    MadTom is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jun 2018
    Location
    CT and VT
    Posts
    206

    Selecting the first Record in a Query

    I have a search form (frmSearch) that returns records from a strSql code in VBA. I would like to select the first record in the unbound list box (lstSearch) after I do a search. I tried to use Me![lstSearch].SetFocus but only underlines the first record and
    DoCmd.GoToRecord , , acFirst doesn't work
    . I would like to have the first record selected as if I clicked on it after I do a search, which would be highlighted. Been searching for code to do this. RowSource is a Select Query and changes with RowSource = strSql statement.


    Thanks!
    Tom

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Try

    Me.lstSearch= Me.lstSearch.ItemData(0)
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    MadTom's Avatar
    MadTom is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jun 2018
    Location
    CT and VT
    Posts
    206
    Thanks, that's it. After hours of searching for one line. But why doesn't the pgup and pgdn keys work? Would like the End, PgUp, PgDn keys to work.

    Tom

  4. #4
    MadTom's Avatar
    MadTom is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jun 2018
    Location
    CT and VT
    Posts
    206
    OK, if I use
    Me![lstSearch].SetFocus the PgUp and PgDn work, but End doesn't work?

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Never tried it, but it doesn't appear to work for me in a listbox that has focus anyway.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    MadTom's Avatar
    MadTom is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jun 2018
    Location
    CT and VT
    Posts
    206

    Work around using Me.lstSearch = Me.lstSearch.ItemData(0)

    Thanks! I added buttons and used
    Me.lstSearch= Me.lstSearch.ItemData(0)
    Now I have what I needed and learned something new.

    Code:
    
    Private Sub btnFirstRecord_Click()
        On Error Resume Next
        Me.lstSearch = Me.lstSearch.ItemData(0)
    End Sub
    
    
    Private Sub btnLastRecord_Click()
        On Error Resume Next
        Dim LastRow As Integer
        LastRow = Me.lstSearch.ListCount - 1
        Me.lstSearch = Me.lstSearch.ItemData(LastRow)
    End Sub

  7. #7
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Happy to help!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Replies: 2
    Last Post: 07-08-2014, 10:03 AM
  2. Selecting a record from a sub form
    By pfales in forum Programming
    Replies: 19
    Last Post: 06-10-2013, 04:32 AM
  3. Selecting only one record for a max of in a design query
    By PhilChamberlain in forum Queries
    Replies: 3
    Last Post: 08-29-2012, 10:15 PM
  4. Replies: 9
    Last Post: 09-16-2011, 03:52 PM
  5. Record Selecting
    By dbprogman in forum Programming
    Replies: 5
    Last Post: 04-27-2011, 10:35 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