Error (2450) when changing a forms recordsource through code

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a form that displays a bunch of information to the user. On the form,
I have two command buttons that fire off a series of actions. The last action
in the click event of each of these buttons is to open a form, which contains
a couple of subforms, the difference being the RecordSource for the two
subforms. When I click one of the buttons, and execute code that looks like
the following, it executes properly. When I click the other button, I change
the record source for the two subforms, but I get an error indicating that
the form cannot be found. I have checked to make sure that the code is
identical, only the name of the queries change between the two subroutines.

Private sub Button1_Click

'do something here
forms("frm_Report_rollup").RecordSource = "qry_rollup"
forms("frm_Report_details").RecordSource = "qry_details"
docmd.openreport "MyReport"

end sub
 
Subforms are not members of the Forms collection. You need to refer to them
via their parent form ...

Forms("NameOfParentForm").Controls("NameOfSubformControl").Form.RecordSource

If the code is running from the parent form, you can shorten this to ...

Me!NameOfSubfromControl.Form.RecordSource
 
Brandan,

At the time of the change in the record source, their parent form has not
yet been loaded. Plus, I would think I would get the same error, regardless
which of the two buttons I clicked, but I don't. With one button, the forms
load properly, with the other, I get the error message. I have not tried
this yet, but now that I think about it, the one that does not generate the
error has the same recordsource as the default for those forms, so maybe it
isn't updating the recordsource in either instance.
 
Subforms, or, to be more specific, forms which are being used as the
SourceObject of a subform control, are not members of the Forms collection,
so if code similar to that which you posted is working, the forms in
question are not being used as subforms in this specific sense - perhaps you
are using the term differently?
 
Brendan,

Got it. I moved the code for the recordsource to the Open event of the main
form, and passed it a value in the OpenArgs parameter of the the OpenForms
method.

Thanks for the help.

Dale
 
Back
Top