Forms and subform

DevAccess

Registered User.
Local time
Today, 13:04
Joined
Jun 27, 2016
Messages
321
Hi there,

I have form and it has subform inside it and now I have save button, since when we were moving focus from form to subform it was asking to save record as we had form before update event to save confirmation so I removed it and on save button i have written docmd.runcommand acsaverecrod.

This works very well when we have new record but when we are opening existing record which is populated on both subform and form and when we are editing and modifing it some content and when we click on save button it ask for want to save or not, even though we click on NO it saves recrod.

on VBYES confirmation I have written docmd.runcommand acsaverecord and else if I click NO it do exit sub.

What could be possible reason..

Thanks
 
Access automatically saves data in bound forms on any event where the focus is moved elsewhere, e.g., moving to a subform, navigating to a different record, or closing the form. In those cases where we wanted to give the user the choice of saving changes or not we found we had to use Me.Undo when changes were to be discarded.
 
you shouldn't have a form and a subform both editing details from the same table. Is that what you have?

in principle the subform is dependent on the mainform, but should have no data from the subform.
 
Access automatically saves data in bound forms on any event where the focus is moved elsewhere, e.g., moving to a subform, navigating to a different record, or closing the form. In those cases where we wanted to give the user the choice of saving changes or not we found we had to use Me.Undo when changes were to be discarded.

So do you mean that is okay to have form before update event to get fired when we move focus from form to subform ?
 
you shouldn't have a form and a subform both editing details from the same table. Is that what you have?

in principle the subform is dependent on the mainform, but should have no data from the subform.

Both have different query
 
Both have different query

that isn't the point.

if the subform is showing data relating to the current item on the main form, then there is no need to show any data from the main form on the subform, and certainly no need to change it. If you need to show it/change it, it should be on the main form. Change data on the main form will affect other items related to the main form, in addition to the one on your subform.
 
that isn't the point.

if the subform is showing data relating to the current item on the main form, then there is no need to show any data from the main form on the subform, and certainly no need to change it. If you need to show it/change it, it should be on the main form. Change data on the main form will affect other items related to the main form, in addition to the one on your subform.

Thanks I have below situation

I have form and subform when I am moving focus from main form to subform event it asks to save data because we have written before update event of the form and if we cancel it wont get saved it works great .

But now I am trying to save data in subform it gets saved ( Please consider we are editing existing data ) so therefore I created one hidden field and and set the value to 0 when form gets load, and when user moves focus from form to subform i am alerting user on on before event of main form to save or not, if user does not save the field value is going to 1 and in subforms on before update event i am checking if the value of the field is 1 then
Code:
 cancel= false and me.undo
.

I also have save button which have code to save form
Code:
 docmd.runcommand acsaverecord
but this code is not get fired if i set continue = false in subforms event.

Can you please help me or can give alternative solution.

Thanks
 
The typical setup of a form and subform facilitates the entry of data in a parent and a child table. The data in the parent table being displayed in the main form and the data of the child in the subform. If the tables in the form and subform are related like this the primary and foreign fields are maintained for you in the subform control's Master Link Fields. If you are entering data into the subform, i.e., the child table you normally want the foreign key of the child table to be the primary key of the parent table. So the data in the main form needs to be saved before the data in the subform can be entered.

Why are you trying to avoid saving the main form data? What is the relationship between the data in the main form and the subform?
 
I am avoiding to test if user cancels it , it should not save the data anyways in the main form and sub form.
 
My concern is that when user clicks no to save data from main form and I am doing on before update event of subform to cancel = true, why the save button code does not work.
 
I don't know what the difference is between docmd.runcommand acsaverecord and DoCmd.RunCommand acCmdSaveRecord but I've always used

Code:
DoCmd.RunCommand acCmdSaveRecord.

I suggest trying that or

Code:
If Me.Dirty Then Me.Dirty = False

as some like to do as described here.
 
Thanks I have below situation

I have form and subform when I am moving focus from main form to subform event it asks to save data because we have written before update event of the form and if we cancel it wont get saved it works great .

But now I am trying to save data in subform it gets saved ( Please consider we are editing existing data ) so therefore I created one hidden field and and set the value to 0 when form gets load, and when user moves focus from form to subform i am alerting user on on before event of main form to save or not, if user does not save the field value is going to 1 and in subforms on before update event i am checking if the value of the field is 1 then
Code:
 cancel= false and me.undo
.

I also have save button which have code to save form
Code:
 docmd.runcommand acsaverecord
but this code is not get fired if i set continue = false in subforms event.

Can you please help me or can give alternative solution.

Thanks

it's not really a matter of having a "button" to save a record.

Access is event driven. Access will try to save a record when certain events happen, and it is hard if not impossible to prevent this behaviour.

So, having a save button is often just a design feature that adds comfort to a user in some cases. The real activity is done on form events.

When certain events happen, the active form will try to save the record. When this happens a number of events fire, in particular the forms before update. This is probably where a developer should concentrate most efforts to manage form activity, and verify that saving the record is appropriate. If you CANCEL this event the record will not save.

Having a button to save a record may well achieve a record save, but other events will also lead to a record save, without the button being pressed.
 

Users who are viewing this thread

Back
Top Bottom