Results 1 to 11 of 11
  1. #1
    Lukael is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2013
    Posts
    239

    Save changes in subform ?

    Hi,



    I don't find answer anywhere so I think It's impossible, but still - can be (in any way) done that once you allready fill some data in subform, Main form be closed with msgbox prompt "Save changes ?", If user doesn't click on save button of subform ?

    I know access automatically saves record in subform when focus is changed to Main form, but what about Lost Focus event or On Exit of Subform, or deleting last saved record in subform If user doesn't click button for save ??

    I tried with some codes, but none was working.


    Any help appreciated !!

  2. #2
    JeroenMioch's Avatar
    JeroenMioch is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2012
    Location
    Den Haag, Netherlands
    Posts
    368
    If Me![YourSubForm].Form = dirty
    'check if the record has been editted
    then
    Dim LResponse As Integer
    LResponse = MsgBox("Save changes ?" _
    , vbYesNo, "Continue")
    If LResponse = vbYes Then
    me.dirty = false
    'save changes
    else
    'if NO was selected
    'code to undo the changes like me![YourSubForm].form.undo (aircode)
    End If
    End If

  3. #3
    Lukael is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2013
    Posts
    239
    And where exactly should this code go into - On Exit (Subform event), Load(Form with subform), Before_Update(Subform event) and Current(Subform event) doesn't work ?

  4. #4
    NTC is offline VIP
    Windows 7 64bit Access 2013
    Join Date
    Nov 2009
    Posts
    2,392
    Access as a windows application saves all edits by default... the write occurs when the record is exited - no matter how i.e. form close, go to another form etc. There is fundamentally no need for a Save button.

    That is the bottom line.

    For some reason some people want there to be a Save button and an option to Undo the entries/edits - but this is purely a construct intended to please an arbitrary design preference.

  5. #5
    Lukael is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2013
    Posts
    239
    NTC,
    thanks for reply. I fully agree with you and I am also aware of Access functionality in regard of saving records, but unfortunally Users of my DB wish to have this "feature", they don't have a slight idea of what focus in subform or main form is. I just want to know If there is a way that I'm not aware of and make It happen, so that they leave me alone for good ☺

  6. #6
    NTC is offline VIP
    Windows 7 64bit Access 2013
    Join Date
    Nov 2009
    Posts
    2,392
    I see it frequently. Your implementation depends on the user interface experience they think they want - and you have a bunch of trigger event choices depending on when they want to do this.

    But basically to save - it is the form's (or sub form's) dirty = false method and that forces the write.

    While to revert you use the Undo method.

    You can find info on both online

    Whether or not that is a command button or a pop up or whatever the trigger point is - is all variable depending on the user experience desired.....

  7. #7
    Lukael is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2013
    Posts
    239
    Yes, but I have a problem. My form has subform, and only subform can be edited, all other main form fields are locked.

    So what I want is only to warn User If he wants to save changes, in case If he closes form without clicking on Save button under subform (This button is inserted in subform footer, so It works). But when you click on close button, or X button, focus goes from subform away, so Dirty and Undo doesn't work anymore - because Access allready saves records. And this is what I'm trying to seek an answer for, If It's possible.

  8. #8
    Lukael is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2013
    Posts
    239
    This is the closest thing I found. Code works, but with one problem - It keeps triggering, even on Load_Event of form. Code must be put in subform. I there any way to prevent this Before_Update event to trigger on Form_Load, and keep It running from then on ?

    Code:
    Private Sub Form_BeforeUpdate(Cancel As Integer)
    
    
    Dim strMsg As String
    Dim intResponse As Integer
    
    strMsg = "You have made one or more changes to this Record. Do you wish to Save this Record " & _
             "with those changes?" & vbCrLf & vbCrLf & "Click Yes to Save changes, or Cancel to " & _
             "UNDO these changes?"
    
    
    intResponse = MsgBox(strMsg, vbQuestion + vbOKCancel + vbDefaultButton1, "Prompt to Save Record")
    
    
    If intResponse = vbCancel Then
      DoCmd.RunCommand acCmdUndo
    End If
    EDIT : Code work on Current_Event too.

  9. #9
    Lukael is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2013
    Posts
    239
    Anybody knows something, or not possible at all ?

  10. #10
    NTC is offline VIP
    Windows 7 64bit Access 2013
    Join Date
    Nov 2009
    Posts
    2,392
    if code at the Before Update event is firing at the On Load event of a form - there must be other aspects of this that are not clear - perhaps some default values getting put in that are triggering the Before Update or possibly even a corruption issue though I don't like to play that card it does often surface as events firing incorrectly.....

    fundamentally however part of your solution is involving being creating in controlling the events that the user can trigger. you might deactivate the X of the main form so they are required to use a command button to close - - and then consider deactivating that command button based upon their status in the sub form.... in this way you can closely control what / when the triggers are fired....

  11. #11
    Lukael is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2013
    Posts
    239
    Code from post #8 is just fine, I just had some problems in Load event, where I was changing some fields value - here was a glitch, so I removed It. This code must be put in BeforeUpdate event, but interesting It works in
    subform Exit event too - only problem is that you can't determine when is subform dirty or changed, so code triggers always when you close form - but undo works. I'm closing this thread, my problem is solved.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Save data in a subform
    By vito1010 in forum Forms
    Replies: 2
    Last Post: 01-01-2015, 06:44 PM
  2. Replies: 15
    Last Post: 01-28-2014, 12:20 PM
  3. Replies: 5
    Last Post: 12-19-2012, 01:05 PM
  4. Save Subform Changes
    By Bennet in forum Access
    Replies: 1
    Last Post: 11-22-2011, 09:23 AM
  5. Replies: 2
    Last Post: 01-29-2010, 11:33 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums