I've recently had a nasty problem with the "Invalid reference to the
property Form" error in subforms - nasty because it doesn't seem to
consistently happen to all forms which contain the same structure and
code. Judging by the forums I've researched, it's not an uncommon
error. I'm happy to say I've found a simple solution.
OVERVIEW: I have a main form (no record source) which contains two
subforms. The subforms are not linked, but there is some code in Sub1
which finds a related record in Sub2. The problem is that the
OnCurrent event in Sub1 runs BEFORE Sub2 has loaded, so the first time
it causes an "Invalid reference to the property Form" error. I trap
for the error, then Sub2 loads, and everything works from this point.
CAUSE: subforms are controls, and they run their events before the
Main forms events. So the sequence of events when I open the Main form
is
Sub1: Open, Load, Activate, Current
Sub2: Open, Load, Activate, Current
Main: Open, Load, Activate, Current
The problem occurs in some forms and not others because of the order I
originally placed the subforms on them!
SOLUTION: Sub2 needs to load before Sub1, so that the code in the Sub1
OnCurrent event can reference it. Unfortunately, there is no load
sequence property for subforms, and the Tab Order property doesn't
affect the sequence of loading. However, you can influence the load
order!
1) remove the subforms from Main and save it. For good measure I also
did a compact and repair
2) add Sub2 to Main and Save it. Being paranoid, I did another compact
and repair
3) add Sub1 to Main and Save it. Now it works and I can get rid of the
error trapping!