Good Morning. I have a problem that I think is syntax. I'm getting an error 2455, which says you have specified an expression that contains an invalid reference to the Form / Report property.
Can I ask why - it is obviously very specific to the form, so why not simply run it from the form?
Then you would be able to refer to the controls directly.
Putting things in modules can be great, but when something is unlikely to be reused anywhere else it makes no sense to me.
Keep it with the form it's updating.
I would suspect you have a spelling error in your form reference. Double-check it, or use the expression builder to build it for you, then cut and paste.
That is a pretty uncommon error, but happens when the subform returns no records. When the subform returns no records the controls are not loaded and thus this error occurs. If you are doing some filtering there could be a timing issue. Which means you want to add DOEVENTS prior to this code running.
Wait, maybe it's not a syntax problem. In the form, I have a question to create a new record. If I say no, that's when I get the error. So I think the error comes because there are no records. Then it would be a matter of "canceling" that error.
That is a pretty uncommon error, but happens when the subform returns no records. When the subform returns no records the controls are not loaded and thus this error occurs. If you are doing some filtering there could be a timing issue. Which means you want to add DOEVENTS prior to this code running.
I can tell you that there are some extremely confusing situations that may cause errors when either a main form or subform returns no records.
A very common one is if you have filter controls in the header and you filter the form based on a control. This normally will cause focus to leave your active control. So any code that references the active control or .text property fails. This one is super confusing because the control you just typed in is not the active control.
If the subform returns no records, the controls are not loaded. But I think the subform is also not loaded. So in this case you cannot reference the control, but it may fail earlier than that by just trying to reference the subform. Untested.
@Isaac,
I would not recommend that anyone without some more context, unless they fully understand the implications of not using it properly. This may cause some hard to understand issues. As shown in that recent thread, the OP showed exactly how not understanding that caused an issue.
The behavior that you pointed out is confusing and is unique to Access AFAIK. Like you said simply by instantiating and referencing an instance of the class it activates an instance in a hidden mode. Since you cannot see it and it goes out of scope no one realizes that behavior. Even in Excel this does not happen you can instantiate an instance of a form class and it does not automatically activate. Same in .NET. So your point of not referencing a form or report by the class is good advice, because of these things that happen that you are unlikely to even realize. You used to see this done a lot for some reason, I have not seen that done for years. In the early days you could not do multiple instances so maybe it made more sense.
In this case it is not applicable anyways since it is a subform. I think it was not called from the main so the OP could not simply
me.[FPresupuestosSubformulario].Form.TxtQuantityAProrratear.BackColor
If you want intellisense as @Isaac is saying, the safe way is you must declare a variable. As @Isaac says this can be very helpful (similar to the discussion on using a module name) because you get intellisense like working directly in the form.
Code:
dim frm as Form_SomeFormName ' does not instantiate the hidden instance
set frm = Forms![FPresupuestos]![FPresupuestosSubformulario].Form
now intellisense
frm......
In this case you get intellisense and all the form properties, but do not end up automatically activating and referencing a hidden form.
This is likely the reason in Access you almost always open a form from the docmd.
The unsafe way would be (assuming not used as a subform)
Form_FPresupuestosSubformulario.someproperty
That may or may not be referencing the form instance you think.