save button on form with subform

jgitaunjoroge

New member
Local time
Today, 02:31
Joined
Apr 18, 2015
Messages
9
Hi Friends,

Would like to have a save button on a form with subform. Access normally saves it data when the record loses focus. but this is not what my employer
wants since it might end up giving garbage.

Below is a screenshot of what i mean.

How do i control saving from the buttons.
 

Attachments

  • customer form.png
    customer form.png
    36.9 KB · Views: 413
Last edited:
If you need only a button for save add event on click
docmd.save
if you need exit and save
docmd.close acform, me.name, acsaveyes

but the cancel button for the subform after you insert more than one record on subform is little bit more complicated
 
All i want is one save button for main and subform to save edited and new data. I have tried to figure it out no solution. Will be very grateful for suggestion given.

thanks,
Njoroge
 
here all information you need about save command
you can also use the wizard for macro record operation - save record but I dont know if it save all dirty record on subform.
vba code for button save is
docmd.save acform, me.name
to add it
design mode
design tab > insert button, cancel on wizard> proprieties (dblclick)>event tab> on click tree dots > code builder > insert the code on cursor position > save and run
 
In the screenshot attached, main form section details are not keyed in. They are populated after combox change. Subform details have to be keyed in, but automatically saved when it looses focus, how do i avoid this scenerio of saving data from subform automatically. I wanted to use save button on main form.
Thanks,
Njoroge
 
You need to not save the record until you press the save button? Right?
 
Please illustrate to me how to do that. should i post the whole database .....
 
now I understand what you need:D
add event Form_BeforeUpdate on your subform
If Forms!mainform.sumform.Form.Dirtyor Or Forms!mainform.sumform.Form.NewRecord Then Forms!mainform.sumform.Form.Undo

this way it will discard all chanage you made
but then your code cmd.save will cancel all the change you made
I made it work that you need this way:
in one module set public save as string
on open of main form set:
save = "no"
on close of main form: the same
on save button added
save = "yes"
on Form_BeforeUpdate of the subforum I added:
if save = "no" then
If Forms!mainform.sumform.Form.Dirtyor Or Forms!mainform.sumform.Form.NewRecord Then Forms!mainform.sumform.Form.Undo
end if
do the same before update event on mainform if you need to not save
but then if they add 2 new record on subform the 1st will be deleted if not press the save button and not sure haw to avoid that...
but this is one way... maybe is a easier way to do and I dont know
 
Access normally saves it data when the record loses focus.

this is true for bound forms. but there is a fix. buffer first all updates/addition until you pressed the "Save" button. in ms access you can accomplished this by using Workspace's BeginTrans, CommitTrans (saving all changes new and old), Rollback (do not commit any changes).

declare a private form variable:

Private WrkSpace As Dao.WorkSpace

on OnLoad event of your form:

Private Sub Form_Load()

Set WrkSpace = DbEngine().WorkSpace(0)
' begin buffering to memory
WrkSpace.BeginTrans

End Sub

on the Click event of your "save" button

Private Sub btnClick_Click()
' save all changes to physical disk
WrkSpace.CommitTrans
' create new transaction
WrkSpace.BeginTrans
End Sub


Private Sub Form_Unload(Cancel As Integer)
WrkSpace.Close
Set WrkSpace = Nothing
End Sub
 

Users who are viewing this thread

Back
Top Bottom