Search code examples
vbams-accessms-access-2013ms-access-forms

Get ID Of Selected Row In Sub Form


I have a Parent Form with a Sub Form and I want the user to be able to select a record from the sub form, then click a button on the Parent Form, which will launch a "new" form that has full demo's pertaining to the selected record from the sub form.

How would I do this in Access 2013?


Solution

  • You can pass the ID as a parameter when opening the "new" form.

    On your button's Click event:

    Private Sub Command0_Click()
        'Get the ID
        Dim id_ As Long
            id_ = Me.SubformName.Form!ID
    
        'Open the new form and pass the ID to the .OpenArgs
        DoCmd.OpenForm "FormName", acNormal, , , acFormPropertySettings, acWindowNormal, id_
    End Sub
    

    On the Form's Load event, check the .OpenArgs and filter the form (or whatever else you need to do) to the supplied ID.

    Private Sub Form_Load()
        With Me
            If Not IsNull(.OpenArgs) Then
                .Filter = "[ID]=" & .OpenArgs
                .FilterOn = True
                .Caption = "ID: " & .OpenArgs
            End If
        End With
    End Sub