Share via

Better code to requery multiple subforms in a form

Anonymous
2015-07-19T06:35:32+00:00

Hello,

I have a form with 5 subforms nested together in one parent form

The code on the activate event of the parent form requeries the first subform1 and subform1 requires subform2 and so on with the requery code on each underlying form's current event.

when I add or edit data, the whole subforms reflect the changes. But when I delete all the displayed records, all subforms are supposed to go blank, but only subform1 does. Unless I requerry all the subforms.

I need a better way to write a code to requery all the subforms at once instead of:

subform1.Requery

subform2.Requery

subform3.Requery

subform4.Requery

subform5.Requery

Thank you all

Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

14 answers

Sort by: Most helpful
  1. Tom van Stiphout 40,211 Reputation points MVP Volunteer Moderator
    2015-07-19T23:00:02+00:00

    How about:

        Dim ctl As Control

        For Each ctl In Me.Controls

            If TypeOf ctl Is SubForm Then

                ctl.Requery

            End If

        Next ctl

    Was this answer helpful?

    3 people found this answer helpful.
    0 comments No comments
  2. Anonymous
    2015-07-19T23:16:09+00:00

    Docmd.showallrecords

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2015-07-19T22:39:24+00:00

    For what it's worth, requerying the mainform will requery all the subforms.

    Of course that will ALSO move the mainform to the first record of the recordset, and probably have other effects as well, so it might not be what you want!

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2015-07-19T21:35:15+00:00

    Thanks for your reply.

    I thought there should be a better way to write the code to perform that action.

    so even if I have up 10 subforms, I will keep writing:

    Subform1.Requery, Subform2.Requery and so on.

    That seems redundant.

    Was this answer helpful?

    0 comments No comments
  5. Tom van Stiphout 40,211 Reputation points MVP Volunteer Moderator
    2015-07-19T13:54:24+00:00

    What would you consider "a better way"? I see nothing wrong with the above code.

    Was this answer helpful?

    0 comments No comments