vbams-accesssubforms

Access Subform Changes Focus on Button Click


I have a subform (continuous form) with a button on each row that filters the main form on that record when clicked. When I click the button, the focus on the subform jumps to the top (i.e. first record) of the subform. How can I make the focus stay on the record.

In other words, I click the button next to record 100, and the focus changes to record one on the subform (main form displays record 100 as intended). I want focus on subform to stay on record 100.

Here's the VBA code I'm using:

Private Sub select_record_button_Click()
Me.Parent.Form.Filter = "[ID]=" & Me.ID
Me.Parent.Form.FilterOn = True
End Sub

The above code is on the On Click event for the button on the subform.


Solution

  • You could try this:

    Hold the subform's .SelTop property to a variable before applying the filter, and restore once the filter has been applied.

    It won't keep the selected record in the exact row but it will be the first record in the subform's visible area.

    Private Sub select_record_button_Click()
        Dim t As Long
        With Me
            t = .SelTop
            With .Parent.Form
                .Filter = "[ID]=" & Me!ID
                .FilterOn = True
            End With
            .SelTop = t
        End with
    End Sub