• Access VBA select a record on a form (2000, 2003)

    Home » Forums » AskWoody support » Productivity software by function » MS Access and database help » Access VBA select a record on a form (2000, 2003)

    Author
    Topic
    #456471

    Hi,

    I have a form based on a query.
    The form’s recordset is filtered by setting its Filter property through some code, which works fine.

    What is the fastest way to find (and select) a record with a specific Id within the filtered recordset?

    Viewing 0 reply threads
    Author
    Replies
    • #1140803

      You can use code like this:

      Dim lngID As Long
      Dim rst As DAO.Recordset

      lngID = …
      Set rst = Me.RecordsetClone
      rst.FindFirst “ID=” & lngID
      If rst.NoMatch Then
      MsgBox “ID ” & lngID & ” not found.”, vbExclamation
      Else
      Me.Bookmark = rst.Bookmark
      End If
      Set rst = Nothing

      The code requires that you have set a reference to the Microsoft DAO 3.6 Object Library.
      I have assumed that the field you’re looking for is named ID and that it is a number field. If it is a text field, use

      rst.FindFirst “ID=” & Chr(34) & lngID & Chr(34)

      • #1140805

        Thanks Hans, looks simple enough.

        • #1140806

          It’s basically the code generated by the Combo Box Wizard if you select the option “Find a record on my form based on the value I selected in my combo box”.

          • #1140809

            Ah. Followup: if I use the scroll wheel to scroll down so the record goes out of view, how do I ensure it comes back when I click my button?

            I tried:

            Me.SelTop = Me.CurrentRecord

            but that doesn’t seem to do the trick?

            • #1140813

              Setting SelTop isn’t very dependable. You could try something like

              Dim n As Long
              n = Me.CurrentRecord
              Me.SelTop = Me.RecordsetClone.RecordCount
              Me.SelTop = 1
              Me.SelTop = n

            • #1140833

              Looks promising, thanks.

            • #1140836

              Hi Hans, what’s the purpose of setting Me.SelTop = Me.RecordsetClone.RecordCount and then Me.SelTop = n?
              I’ve tried it on an unfiltered continuous form where the current record on opening is 24 rows from the bottom and Me.RecordsetClone.RecordCount and Me.CurrentRecord give me the same number.

            • #1140837

              In a form with only a few records, setting SelTop has little effect anyway. In a form with more records, setting SelTop to RecordCount and then to 1 forces Access to jump. The value of n is the current record number before the jumps.

    Viewing 0 reply threads
    Reply To: Access VBA select a record on a form (2000, 2003)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: