how to select a record in a subform datasheet using code in main form. MS access 2016

how to select a record in a sub-form datasheet using  code in main form?  MS access 2016

using code behind main form:
e.g. I want to select the sub form id 200 in the datasheet.
when I push a button on the main form.

Who is Participating?
Gustav BrockConnect With a Mentor CIOCommented:
This should do:

Private Sub YourButton_Click()

    Dim rst As DAO.Recordset
    Dim Id As Long

    If Not Me.NewRecord Then
        Id = Me!Id.Value
        With Me.YourSubformControlName.Form
            Set rst = .Recordsetclone
            rst.FindFirst "[ID] = " & Id & ""
            If rst.NoMatch = False Then
                .Bookmark = rst.Bookmark
            End If
        End With
    End If

    Set rst = Nothing

End Sub

Open in new window

Rey Obrero (Capricorn1)Commented:
try this codes

private sub button_click()
with me.subformcontrolName.form.recordsetclone
     .findfirst "[ID]=200"
     if not .nomatch then
     end if

end with
end sub

for dynamic  record id selection, use a control In the main form that display the "[ID]" number
johnAuthor Commented:
Only Gustav Brock's solution worked.  thank  you
Rey Obrero (Capricorn1)Commented:
what  was wrong with the code I posted?
Gustav BrockCIOCommented:
Rey, are you serious? Too little coffee? Look again.

All Courses

From novice to tech pro — start learning today.