Error: "...invalid reference to the property Form.."

G

GeoBrooks

I have a form on which there are two tabs, one for mothers, one for babies.
The babies tab contains a subform in which new records may be added. The
related code snippet is:

Forms!frmmomsbabies!ctlSubBabies.SetFocus
DoCmd.GoToRecord , , acNewRec
Forms!frmmomsbabies!ctlSubBabies.Form!mom_id = intMomId
Forms!frmmomsbabies!ctlSubBabies.Form!txtBaby.SetFocus

This snippet worked, and now doesn't, and I cannot figure out what I might
have done to cause this. [Oh, for a revert to history!] The line where a
control's value is set (Forms!frmmomsbabies!ctlSubBabies.Form!mom_id =
intMomId) is the one that now gives the error "You entered and expression
that has an invalid reference to the property Form/Report." In debugging,
typename(Forms!frmmomsbabies!ctlSubBabies) is SubForm, but appending .Form to
it yields an error.

Any & all clues are more than welcome.

TIA.

George
 
G

GeoBrooks

The offending line of code was Me.Requery just two lines above. So now the
question is, "Why does a requery louse up the control reference?"

gwb
 
A

Allen Browne

Perhaps it's a timing issue, or perhaps the subform control is losing its
form, or perhaps something else is interfering.

Me.Requery will trigger several things, including a save of the current
record, and a reload of the currrent form's data which takes you back to the
first record (if there is one) or the unloading of the form (if its Data
Entry property is Yes, or a filter rejects newly added records.) If no new
records can be added (e.g. AllowAdditions is No, or source is a read-only
query), the form's Detail section will now go blank, and Access will have
trouble handling references to other controls on the form.

If that's not the problem, the main form finds the first record, and its
Current event fires, and the subform is reloaded to match the record in the
main form. If this causes some other interaction with the subform (such as
action in the subform's Current event), other things could happen (even an
endless loop.)

For debugging purposes, perhaps you could add lines like:
Debug.Print Me.NewRecord
Debug.Print Me.ctlSubBabies.SourceObject
Debug.Printe Me.ctlSubBabies.Form.NewRecord
Then when it fails, press Ctrl+G to open the Immediate Window, and see if it
makes sense.

Ultimately, there may be another way to approach this. For example, if you
are trying to add a new record to the subform without any user interaction,
it may be possible to AddNew to its RecordsetClone (with Update.)

--
Allen Browne - Microsoft MVP. Perth, Western Australia

Reply to group, rather than allenbrowne at mvps dot org.

GeoBrooks said:
The offending line of code was Me.Requery just two lines above. So now
the
question is, "Why does a requery louse up the control reference?"

gwb

GeoBrooks said:
I have a form on which there are two tabs, one for mothers, one for
babies.
The babies tab contains a subform in which new records may be added.
The
related code snippet is:

Forms!frmmomsbabies!ctlSubBabies.SetFocus
DoCmd.GoToRecord , , acNewRec
Forms!frmmomsbabies!ctlSubBabies.Form!mom_id = intMomId
Forms!frmmomsbabies!ctlSubBabies.Form!txtBaby.SetFocus

This snippet worked, and now doesn't, and I cannot figure out what I
might
have done to cause this. [Oh, for a revert to history!] The line where
a
control's value is set (Forms!frmmomsbabies!ctlSubBabies.Form!mom_id =
intMomId) is the one that now gives the error "You entered and expression
that has an invalid reference to the property Form/Report." In
debugging,
typename(Forms!frmmomsbabies!ctlSubBabies) is SubForm, but appending
.Form to
it yields an error.

Any & all clues are more than welcome.

TIA.

George
 
G

GeoBrooks

Allen,

Thanks for the reply. The explanation of Requery makes a fair bit of sense,
at least in terms of control references.

I added these lines in place of the earlier quoted lines
For debugging purposes, perhaps you could add lines like:
Debug.Print Me.NewRecord
Debug.Print Me.ctlSubBabies.SourceObject
Debug.Print Me.ctlSubBabies.Form.NewRecord
Then when it fails, press Ctrl+G to open the Immediate Window, and see if it
makes sense.

and got:
0
frmBabies
-1

Which I think says "New record now? No; Form is frmBabies; New record now?
Yes"

Here's a description of what got me into trying Requery:

Situation:
I have a form for mothers & babies. There are two combo boxes in the header,
one for selecting each. There's also a button for adding a mother. The detail
section has two tabs, one for mothers, one for babies. The mothers tab is
simply additional fields from the recordset. The babies tab is a subform.
When the main form is first opened, all mom names appear in its combo box,
all babies names appear in its box. Selecting either one sets the focus on
the appropriate record in the proper tab.

Problem:
When a mother is added and its record is saved to add a new baby, the baby
subform is opened. The new baby is entered and that record is added. The
combo boxes show the names as expected. However, when the new baby's name is
selected from the combo box, the subform opens with a blank record. When the
baby's mom's name is selected, her record appears properly in the mom tab,
but the baby tab is again blank. The form must be closed and reopened for the
expected behavior to occur.

The subform's Save command is"
If Me.Dirty Then
DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
End If

George
 
G

GeoBrooks

Solution found. I love abductive logic = wander around aimlessly hoping for
luck or inspiration!

I added "rs.requery" in the babies combo box AfterUpdate event immediately
after setting rs=Me.RecordsetClone. I just needed the requery in the right
place.

gwb
 

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