Refresh form from another form

ilanray

Member
Local time
Today, 02:34
Joined
Jan 3, 2023
Messages
129
Hi
I have a form with some fields and sub form something like outlook design. In the subform , when I click on a certain field, all data of the field apperas on the main form.
If I want to update the form I have a botton which popup another form with all the flelds.
when I save the popup form I would like to update the field on the main form.I tried to use
Code:
[forms]![main_menu]![NaviationSubForm].Requery
the fields are in the NaviationSubForm form
Anyone have an idea for me please?
 
First, welcome to the forum (since that is your first post).

Now, to clarify this, if you are running this code from the same form that contains the button you mentioned, it is just Me.Requery to refresh the form. BUT if this code were running from the subform then it might be something like Me.Parent.Requery (since the parent of a sub-form is the form that contains it.)

However, it sounds like you have a rather complicated setup involving a form, a sub-form, and a pop-up form to save data that SOUNDS like it appears in all three places. Understand that it is your database, so all I can do is offer suggestions and thoughts - but what you described to use sounds a bit excessive. I think the term "overkill" applies, meaning you are doing far more than is necessary. But from your limited description it is hard to know which part - if any - actually is excessive.
 
Thanks for te anser. just to clarify I am trying to run this code from the popup form. after I close the popup form I would like to see the cganges I made in the other form
 
OK, from the popup you have to consider what is visible and from where. The simples correct syntax might be something like that shown below, but you need to know the name of the form you want to requery.

Code:
Forms("name of chosen form").Requery

From the Pop-Up form, the main form is still Me.Parent, so that is still the right syntax to get to that form.

However, that third form, the sub-form, is now a SIBLING of the popup if that popup was launched from the main form or a child of the sub-form if it was launched from the sub-form. In the sibling case, the syntax above is correct, or you could use the more complex form:

Code:
Me.Parent.[Sub-form's control's name].Form.Requery

And note that the actual name of the form does NOT appear in this offering - but the name of the control that holds the sub-form DOES appear. On a further note, it is situations like this, where confusion would be possible, where you do not name the sub-form control the same as the name of the actual sub-form.
 
You never want to have the pop up requery another form unless you pass that form reference to the pop up. If not you are tightly coupling the design.
The form that calls the pop up should execute the code to requery.
This is done using ACDIALOG argument in the docmd.openform when opening the pop up. That will stop code execution in the calling form until the pop up closes. Then in the calling form after the code to open the pop up put the code to requery. That next line of code only executes after the pop up closes.
 
 
well I tried it and it didn't work. when to popup form appears and I close this form, how does it know wich is the parent form?
and another question. in the main form I have navigation option which mean inside the main form I have few navigation button.
 
You tried what? Not helpful.
The form calling the pop up knows what form to requery. If the subform calls the pop up then it can requery itself using me.requery or the main form using me.parent.requery.
 
If the main form calls the pop up it can requery itself using me.requery. it can requery a subform using me.subformcontrolname.form.requery.
 
Assuming you open the popup as a Dialog, then, in the line on the mainform following the OpenForm method, just requery the main form:

Me.Requery

This works because when you open a form in Dialog mode, code in the calling form stops immediately and does not resume until the popup closes.

@The_Doc_Man Only a subform has a Parent property. The mainform is NOT the parent of a Popup. The popup is a mainform that has no parent.

@ilanray Welcome, I agree with Doc, you are very likely making this more complicated than it needs to be. Data should not be stored in multiple places. And if you are opening two forms bound to the same data, you are asking for trouble and may in fact get a conflict error. Because, you are conflicting with yourself.

ALWAYS save the current record in any form BEFORE opening another form/report/etc. That will ensure the current record is not dirty and therefore minimize the potential for conflicts.
 

Users who are viewing this thread

Back
Top Bottom