Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    deepaksharma is offline Competent Performer
    Windows 7 32bit Access 2016
    Join Date
    Jul 2023
    Location
    india
    Posts
    389

    Sub form coding for stop dirty

    I have a form which has a save button and a sub form. When the user opens an old record and changing anything in the sub form And without clicking on the save button, if you close the form then the data of the sub form gets auto saved. so I want to do the coding in the before update event of the sub form.
    That if the sub form is dirty and the save button on the main form is not clicked then a dialog box of Yes No should be shown, if the user clicks on Yes then the data gets saved otherwise the data entry gets undone, what will be the coding for this.


    Thanks.

  2. #2
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    You have to put the code for this in the Subform's before update event.
    Nothing else will capture it, for the reasons you have stated.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  3. #3
    deepaksharma is offline Competent Performer
    Windows 7 32bit Access 2016
    Join Date
    Jul 2023
    Location
    india
    Posts
    389
    Quote Originally Posted by Minty View Post
    You have to put the code for this in the Subform's before update event.
    Nothing else will capture it, for the reasons you have stated.
    Can't the sub form's Before Update event be coded to check whether the Save button on the main form has been clicked or not?

  4. #4
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    This one seems to be the same issue asked over and over. You've been given plenty of answers, including using staging tables. Rather than build these tables yourself and implementing the solution you expected me and/or others to build that for you. People are going to start ignoring your threads if you continue on this path.

    Data auto save problem with sub form
    https://www.accessforums.net/showthread.php?t=88388

    Data undo problem
    https://www.accessforums.net/showthread.php?t=88729

    Vba code "Only for experts"
    https://www.accessforums.net/showthread.php?t=88789

    Sub form coding for stop dirty
    https://www.accessforums.net/showthread.php?t=88859
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  5. #5
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,938
    Already on my IL.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  6. #6
    deepaksharma is offline Competent Performer
    Windows 7 32bit Access 2016
    Join Date
    Jul 2023
    Location
    india
    Posts
    389
    Quote Originally Posted by Micron View Post
    This one seems to be the same issue asked over and over. You've been given plenty of answers, including using staging tables. Rather than build these tables yourself and implementing the solution you expected me and/or others to build that for you. People are going to start ignoring your threads if you continue on this path.

    Data auto save problem with sub form
    https://www.accessforums.net/showthread.php?t=88388

    Data undo problem
    https://www.accessforums.net/showthread.php?t=88729

    Vba code "Only for experts"
    https://www.accessforums.net/showthread.php?t=88789

    Sub form coding for stop dirty
    https://www.accessforums.net/showthread.php?t=88859
    Looks like you're very upset with me
    I have only asked for a code that applies to one condition.

  7. #7
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    Nope, not upset at all. Just making sure others are aware of the situation in case they want to take it under consideration.
    As for "one condition", they all look like the same problem or at least contain elements of the same problem - and they contain viable solutions yet here we still are. When and if you move on from this problem I will probably engage if I can help but I'm not going to repeat here what I and others have said about undoing or preventing form or subform edits.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  8. #8
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    Quote Originally Posted by deepaksharma View Post
    Can't the sub form's Before Update event be coded to check whether the Save button on the main form has been clicked or not?
    I'll give you one more bit of a steer....

    NO - You could check If Me.Parent.Dirty = True, BUT that in theory would never be true because as soon as you move to the Subform the parent form will save the underlying record main record.

    So, you are back to making sure your main record is correct before allowing access to the sub-form.
    And also making sure that you validate the Subform data in a similar manner before leaving it.

    I'm going to repeat myself and other posters, you could nail this down with staging tables, or unbound forms, but the added work is substantial.
    You'd be much better off to either allow users to edit things and trust they know what they doing as they are paid to do it, or have a supervisor to check the data and correct it.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  9. #9
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    but the added work is substantial.
    Not so much for staging tables. All it takes is code that checks if the button was ever clicked or not when form(s) close. If not, close and run delete query. If yes, close and run either append query for new record or update query for existing record, then delete query to delete temp data. Don't have to bother with form BeforeUpdate events for this at all.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  10. #10
    deepaksharma is offline Competent Performer
    Windows 7 32bit Access 2016
    Join Date
    Jul 2023
    Location
    india
    Posts
    389
    Ok I understand there is no such method.
    Now I have done this coding to prevent data editing in BeforeUpdate event of sub form.

    Code:
    Private Sub Form_BeforeUpdate(Cancel As Integer)
    
        If Me.Dirty = True Then
        If MsgBox("are you sure to update the record?", vbYesNo) <> vbYes Then
        Me.Undo
        Cancel = True
        End If
        End If
    
    End Sub
    This coding is working properly but there are two problems in it.
    First of all, the message box is being shown even when the user enters a new record, whereas the message box should be shown only while editing an old record.
    The second problem is that as soon as the user makes corrections in one line of the sub form and goes to the next line, the message box is shown, then after completing the next line, the message box is shown again.
    Is there any way to show the message box only once when leaving the sub form

  11. #11
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    Quote Originally Posted by deepaksharma View Post
    First of all, the message box is being shown even when the user enters a new record, whereas the message box should be shown only while editing an old record.
    You can check if you are on a new record in the code.

    The second problem is that as soon as the user makes corrections in one line of the sub form and goes to the next line, the message box is shown, then after completing the next line, the message box is shown again.
    Is there any way to show the message box only once when leaving the sub form
    No, because you have changed one of the sub form records, and you want to prevent changes to a record without confirmation of saving those changes.
    It's Chicken and Egg, you can't selectively impose rules on record updates, unless you write a lot of code or use staging tables.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  12. #12
    deepaksharma is offline Competent Performer
    Windows 7 32bit Access 2016
    Join Date
    Jul 2023
    Location
    india
    Posts
    389
    Ohh thank you very much for explaining.

    =Minty;517938]You can check if you are on a new record in the code.
    Can you tell the coding of this.
    thank you.

  13. #13
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    You should learn to google a little bit....

    If Me.NewRecord

    https://learn.microsoft.com/en-us/of...Form.NewRecord
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  14. #14
    deepaksharma is offline Competent Performer
    Windows 7 32bit Access 2016
    Join Date
    Jul 2023
    Location
    india
    Posts
    389
    thank you,

    "If Me.NewRecord =true/false" is work fine...

    and https://learn.microsoft.com/en-us/of...Form.NewRecord related to access 95-97

  15. #15
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,938
    Quote Originally Posted by deepaksharma View Post
    thank you,

    "If Me.NewRecord =true/false" is work fine...

    and https://learn.microsoft.com/en-us/of...Form.NewRecord related to access 95-97
    I sincerely doubt that, as it states Article 01/22/2022
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Form is Dirty as soon as I open it
    By mainerain in forum Access
    Replies: 3
    Last Post: 09-04-2022, 08:47 AM
  2. Why is this section of the form dirty
    By Lou_Reed in forum Access
    Replies: 4
    Last Post: 01-25-2017, 03:51 PM
  3. use text value when form is dirty...
    By gangel in forum Forms
    Replies: 2
    Last Post: 06-29-2015, 01:15 AM
  4. On Dirty fires but Me.Dirty = False (v2010)
    By Rod in forum Programming
    Replies: 5
    Last Post: 07-30-2011, 08:42 PM
  5. Replies: 2
    Last Post: 11-29-2009, 12:00 PM

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