No error if opening a form BUT VBA error if I go from Design View to Form View (1 Viewer)

charsiufan

New member
Local time
Today, 16:17
Joined
Jun 26, 2021
Messages
3
I have a form with VBA on the Open event. It is setting the value of a combo box control on an embedded subform:

Me.subfrmShoppingListItem.Form.cboShoppingList = Nz(GetDefaultValue(5))

This works fine whenever I open the form from scratch.

HOWEVER if I place the main form in Design View and then move to Form View I get the 2455 error:

'You entered an expression that has an invalid reference to the property Form/Report.

If I interrogate the value of the control in the Immediate window (as below) at this point it again has the 2455 error:

Immediate Window:

?Me.subfrmShoppingListItem.Form.cboShoppingList

Is it a corrupt form?
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 16:17
Joined
May 7, 2009
Messages
13,693
move your code from Open event to Load event.
on Open event the controls do not exists yet.
 

charsiufan

New member
Local time
Today, 16:17
Joined
Jun 26, 2021
Messages
3
move your code from Open event to Load event.
on Open event the controls do not exists yet.

Thanks but why would it work simply opening the form but gives an error if open in design view first?
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 09:17
Joined
Jul 9, 2003
Messages
13,361
I would suspect that it's because when you switch to design view then your subform is not loaded, it's not present or available for the code to operate on.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 03:17
Joined
Feb 28, 2001
Messages
20,629
The simple test for what Uncle Gizmo is suggesting is after you handle/dismiss the error following the change of modes, can you see the subform in the sub-form control? If you can't see it, it is not loaded. If it is not loaded, you can't act on it.

My question is whether any other form of yours shares that same sub-form. If nobody ever calls the same sub-form, i.e. it ONLY appears as a sub-form of that one main-level form, reverse your logic and have the sub-form's OnLoad event grab the value you want from its parent form. (The syntax to do this starts with Me.Parent.)

It is not that you can't reach INTO a child form but rather that, since you are having the trouble you describe, perhaps you should have the child form reach OUT to get the desired data. Then, if the child doesn't exist, no reaching is done. Of course, that doesn't resolve the question of why the child form doesn't load when you switch to Form View. However, at least part of that might be because if you switch modes like that, you are not starting with a clean context. Loading directly into Form View gives you a "blank slate" but switching from Design View starts with some history.

I have seen many times that when I am debugging and I get a nasty little bug, I can switch to design view. So I diddle with something as an attempt to fix the bug. When I switch back to form view, it remembers values from the last time I was in Form View as long as I didn't do a RESET.
 

charsiufan

New member
Local time
Today, 16:17
Joined
Jun 26, 2021
Messages
3
The simple test for what Uncle Gizmo is suggesting is after you handle/dismiss the error following the change of modes, can you see the subform in the sub-form control? If you can't see it, it is not loaded. If it is not loaded, you can't act on it.

My question is whether any other form of yours shares that same sub-form. If nobody ever calls the same sub-form, i.e. it ONLY appears as a sub-form of that one main-level form, reverse your logic and have the sub-form's OnLoad event grab the value you want from its parent form. (The syntax to do this starts with Me.Parent.)

It is not that you can't reach INTO a child form but rather that, since you are having the trouble you describe, perhaps you should have the child form reach OUT to get the desired data. Then, if the child doesn't exist, no reaching is done. Of course, that doesn't resolve the question of why the child form doesn't load when you switch to Form View. However, at least part of that might be because if you switch modes like that, you are not starting with a clean context. Loading directly into Form View gives you a "blank slate" but switching from Design View starts with some history.

I have seen many times that when I am debugging and I get a nasty little bug, I can switch to design view. So I diddle with something as an attempt to fix the bug. When I switch back to form view, it remembers values from the last time I was in Form View as long as I didn't do a RESET.

Thanks, actually the subform has no dependency on the parent. Its just setting the Combo to the value it was last set to to filter the records in the subform. I was just curious what would trigger this odd behaviour. Good point re setting it in the subform itself. Will give that a go if this persists.
 

Users who are viewing this thread

Top Bottom