Link to home
Start Free TrialLog in
Avatar of SOOPERDAD
SOOPERDAD

asked on

Subform updates on close, cannot get data from field on main form

Hello experts,

This should be pretty simple... I have a subform that has vba code attached that gets data from the main form for use in a string. The code runs on the beforeupdate of the subform and works just fine except when it runs as I close the main form. I'm guessing that somehow the main form is closing before the subform can grab the data. I don't want to change the way I'm getting the data because of various issues, but instead need to know exactly what happens to the form/subforms when the main form closes. If I have this information I think I'll be able to shift things around, add some code, and fix the problem.

Thanks to all,

Chris
Avatar of 0tacon
0tacon
Flag of United Kingdom of Great Britain and Northern Ireland image

The before update event runs for subforms when you close parent forms.

To avoid your before update event running when you close the parent form, you can use a variable to 'tag' the close event of the main form, add vb code to check whether the variable has been set when the before update event of the subform runs- if so, exit the sub routine

-0tacon-
i.e
in the close event for the parent form set the variable to 1

in the before update event of the subform, check to see if variable = 1, if it does, the form is being clsoed, so exit the sub and dont run the code

-0tacon-
Alternatively, on the close event of your main form, you could try using the code:

nameofsubformobject.sourceobject=""

Thus closing the subform first

-0tacon-
Avatar of peter57r
Hello SOOPERDAD,

From MS Docs:

Working with subforms
When you open a form containing a subform, the subform and its records are loaded before the main form. Thus, the events for the subform and its controls (such as Open, Current, Enter, and GotFocus) occur before the events for the form. The Activate event doesn't occur for subforms, however, so opening a main form triggers an Activate event only for the main form.

Similarly, when you close a form containing a subform, the subform and its records are unloaded after the form. The Deactivate event doesn't occur for subforms, so closing a main form triggers a Deactivate event only for the main form. The events for the controls, form, and subform occur in the following order:

Events for the subform's controls (such as Exit and LostFocus)

Events for the form's controls (including the subform control)

Events for the form (such as Deactivate and Close)

Events for the subform
------------------------------------------

Without knowing more detail I would be inclined to look at setting Global variables to hold the info that is being passed.


Pete
Avatar of SOOPERDAD
SOOPERDAD

ASKER

Anyone have any idea why my beforeupdate event for the subform is firing three times when scrolling through records on the main form? I do have a lot of oncurrent events on the subform that alter the data to ensure it is correct, but i could only see that making the beforeupdate event fire once,, not three times... ??

Chris
When scrolling through, do you mean changing records in the main form or using the scroll bar?

If you are using DLookup functions in the main form/subform then this may delay subsequent events
If the code in the on current event in your subform changes/manipulates data this will also fire the update events

-0tacon-
Try using an apostrophe [ ' ] to rem out lines of code to isolate the offenders- or use break points.

At any point data in the subform is being maniuplated, or the subform as a form being manipulated  the update event will be firing..

-0tacon-
How would I force the subform to close when a user clicks the main form close button? I've triend docmd.close "subform" but nothing happens... Today is a retarded day for me....
SOOPERDAD,

I don't believe you can close the subform.
What you can do is remove it and it will close on its own, and run its close event procedure.

 Me.subformcontrolname.SourceObject = ""


Pete
SOOPERDAD- I outlined the above in an earlier post [Date: 06/19/2004 12:33PM GMT]

Setting the recordsource to "" closes the form

-0tacon-
*recordsource=sourceobject
ASKER CERTIFIED SOLUTION
Avatar of 0tacon
0tacon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Otacon,

That works great, I'm assuming what it does is temporarily "disconnect" the two forms so that the subform can be closed? I've never used the .sourceobject (method?) before, so I didn't catch on the first time you said it. Thank you for the perfect answer!

Chris
Thats okay, yes basically it removes the connection between the two forms so any future potential interaction does not occur.

The sourceobject is the form/query loaded into the subform, when you set it to "" you technically empty the subform object of any loaded form or query, thus closing the object currently loaded inot it.

When you have complex parent/child relationships its often a good way forward :)

Good luck with the database

-0tacon-