Subform recordset reference problem

D

Dennis Snelgrove

I've got a form called "frmMainForm" with a subform called
"chldCashDistribution". I'm trying to change the recordset to one of
two different queries, based on the value of a control on the main
form. I've tried referencing the RecordSource like this:

If chkGenerated = False Then
Form_frmMainForm.chldCashDistribution.Form.RecordSource =
"qryCashDistribution"
Else
Form_frmMainForm.chldCashDistribution.Form.RecordSource =
"qryCashDistributionSummary"
End If

but I keep getting the following error:
Run-time Error #2455: You entered an expression that has an invalid
reference to the property Form/Report.

What am I doing wrong?

Thanks for any help...
 
M

Marshall Barton

Dennis said:
I've got a form called "frmMainForm" with a subform called
"chldCashDistribution". I'm trying to change the recordset to one of
two different queries, based on the value of a control on the main
form. I've tried referencing the RecordSource like this:

If chkGenerated = False Then
Form_frmMainForm.chldCashDistribution.Form.RecordSource =
"qryCashDistribution"
Else
Form_frmMainForm.chldCashDistribution.Form.RecordSource =
"qryCashDistributionSummary"
End If

but I keep getting the following error:
Run-time Error #2455: You entered an expression that has an invalid
reference to the property Form/Report.


First, you should not be using the Form_frmMainForm kind of
reference. That is for a different kind of situation. In
this case you can just use:

Me.chldCashDistribution.Form.RecordSource="qryCashDistributionSummary"

but I don't think that's the problem here. Double check
that the name of the subform **control** on the main form is
named chldCashDistribution or change the reference to use
the name of the control.

Me.nameofsubformcontrol.Form.RecordSource= . . .

[Note that the name of the form object displayed in a
subform control may have a different name than the control.
The form object the control is displaying is specified in
the SourceObject property, which is of no interest here]
 
D

Dennis Snelgrove

Actually, I had to reference the subform container that way, because
this piece of code is located in a second subform. I originally used
this code "Parent.chldCashDistribution.Form.RecordSource", and it
didn't work either. I moved to the direct reference as a result, in
order to remove any ambiguity.

Yes, the name of subform container itself is indeed
"chldCashDistribution". I use "chld" to denote the container, since I
name the subforms inside with the prefix "subf".

Is it possible that this is a case of corruption of some kind?
 
M

Marshall Barton

Dennis said:
Actually, I had to reference the subform container that way, because
this piece of code is located in a second subform. I originally used
this code "Parent.chldCashDistribution.Form.RecordSource", and it
didn't work either. I moved to the direct reference as a result, in
order to remove any ambiguity.

Yes, the name of subform container itself is indeed
"chldCashDistribution". I use "chld" to denote the container, since I
name the subforms inside with the prefix "subf".

Is it possible that this is a case of corruption of some kind?



Well, anything is possible if corruption is involved, but
let's not be too quick to jump to that conclusion.

If the the code is in a separate form from the
mainform/subform, then the reference should be:

Forms!frmMainForm.chldCashDistribution.Form.RecordSource

BUT, frmMainForm must have been open prior to all this. If
you just issued the OpenForm method for frmMainForm, then it
may be too early to try to set the RecordSource property.
In this case, it's best if the subform set it itself in its
Open event. Try passing the query name in the OpenForm
method's OpenArgs argument. The subform can then use:
Me.RecordSource = Parent.OpenArgs
to do the job.
 
D

Dennis Snelgrove

I checked this step by step, and it's the ".Form" aspect that's kicking
out at me. For some reason, I can reference the RecordSource from the
main form, but not from another subform. Weird...
 
M

Marshall Barton

Dennis said:
I checked this step by step, and it's the ".Form" aspect that's kicking
out at me. For some reason, I can reference the RecordSource from the
main form, but not from another subform. Weird...


I'm pretty sure that means that either the subform control
name is something other than what's in your reference or
that the subform displayed by the control has not completed
its intantiation (i.e. it's still being opened).

Don't forget that when you use a breakpoint, you change the
timing of various activities so you will not be getting a
true picture of what's happening. For example, the
sequence:
DoCmd.OpenForm "mainform"
Forms!mainform.subform.Form... = ...
may appear to work if a breakpoint is placed on the second
line, but the line will fail in normal operations because
the referenced objects have not completed their
initialization.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top