how to save the form record with vba outside of the form

J

Jay

Does anyone know using vba code to save a changed record in a form that is
not the current form?
For example, formA has a button that opens formB. FormB has a button that
changes some data on formA. After making this change, I want to save the
record in formA (using the code in formB).
I have tried forms!formA.refresh, etc. I have tried docmd.domenuitem (save
entry), etc., but no success. Unfortunately, cannot close and reopen formA.
Thanks very much if you can help.
 
K

Klatuu

This seems like a hard way to do something fairly simple. Why are you
opening form B at all? If you are just changing data on form A, why not just
change it in form A? Even if there is a valid reason (and there surely could
be) to have form B, then unless there is something not apparent, there sould
be no reason to have to save form A. That is assuming, your method of change
the data in form A from form B is something like:

Forms!FormA!SomeContol = Me.MyOwnControl

Once the controls in form A have been updated and you navigate away from the
current record in form A, it will update it.

Perhaps, if this doesn't help, you could post more detail on exactly what
you are doing, we can give a better answer.
 
J

Jay

Thanks Klatuu. My description of formA to formB, then formB updating formA
is my requriement, there is no way around this. To be more specific, formB
is a popup, that updates a textbox on formA when formB closes. Immediately
after formB closes, the user could hit a button that runs a report - this
report should have the updated data. if the user instead changes other data
on formA prior to running the report, it has all the changed data. It is
only when the user immediately runs the report after closing formB that has
the problem where the cfhange from formB does not appear in the report.
Also unfortunately, my total requirements mean that it is more common that
the user runs the report immediately after closing formB than after changing
additional formA data. (so my issue is more than just making it
bullit-proof).
What code could I use when formB closes to save the formA record? Therer
must be a way...
Thanks again.
 
J

John Vinson

What code could I use when formB closes to save the formA record? Therer
must be a way...

Try:

Forms![FormA].Dirty = False

or else (probably better) put code in the event which calls FormB;
after returning from FormB set Me.Dirty to False.

John W. Vinson[MVP]
 
K

Klatuu

immediately after the line in Form A the opens Form B Put this:

Me.Dirty = False
Me.Refresh

The first line updates the form's recordset and the second makes sure the
underlying table is uipdated.
 
J

Jay

I tried the dirty and refresh, both in formA immidately after openning formB
(this one makes no sence to me as nothing has happenned yet), and I tried
immediately after closing formB (still this makes no sense as "dity = false"
would mean, "nothing has changed", no?) Maybe I was not clear originally -
when formB closes, it does forms!formA!myField = forms!formB!myField.
Anyway, both did not work. After closing formB, immediately run a report
and the changes from formB are not included.
Thans again if you can still help
 
K

Klatuu

still this makes no sense as "dity = false" would mean, "nothing has
changed", no?)
Not correct. Me.Dirty = False will update all changes in the form to the
forms recordset.

forms!formA!myField = forms!formB!myField
Which form are you doing this in? It should be in Form B and both forms
should be open.

For this to work correctly, Form B has to be a modal form. If it is not,
then the Me.Dirty and Me.Refresh are happening too soon. That is because the
code will continue to run even after you have opened form B except if form B
is a modal form, then the code will pause until form B closes.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top