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

    Data undo problem

    I have a main form which has 1 sub form.


    When I open an old record in this form for viewing or editing or open a new record, the data should not be auto saved, so I have coded to delete the current data in the form unload event ( If the user does not click on the save button, the form data will be deleted)
    Now the problem arises that if the user opens an old record and closes the form without clicking on the save button, then that record gets deleted.
    I want that the data should not be deleted and it should be undo or the record set of the main table which is linked to the sub table should be undo. What coding can I use for this?
    My coding as under for (current form, unload form and save button)

    Option Compare Database
    Private ASAVED As Boolean
    Dim DB As DAO.Database

    Private Sub Form_Unload(Cancel As Integer)
    Set DB = CurrentDb
    Set RST = DB.OpenRecordset("main_table")

    If ASAVED = False Then

    RST.MoveFirst
    Do Until RST.EOF
    If RST.Fields(1) = Me.ID Then
    RST.Delete
    Exit Sub
    End If
    RST.MoveNext
    Loop
    End If
    End Sub


    Private Sub Form_Current()
    ASAVED = False
    End Sub

    Private Sub SAVE_Click()
    ASAVED = True
    DoCmd.RunCommand (acCmdRecordsGoToNew)

    End Sub

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    If you do not want the data amended (after all it already exists) then check for Me.Dirty and then use Undo if record is Dirty. Or set AllowEdits = False.

    Deleting data, like you are doing is just wrong.

    Learn how Access works, and work with it, rather than trying to fight it.
    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

  3. #3
    deepaksharma is offline Competent Performer
    Windows 7 32bit Access 2016
    Join Date
    Jul 2023
    Location
    india
    Posts
    389
    Quote Originally Posted by Welshgasman View Post
    If you do not want the data amended (after all it already exists) then check for Me.Dirty and then use Undo if record is Dirty. Or set AllowEdits = False.

    Deleting data, like you are doing is just wrong.

    Learn how Access works, and work with it, rather than trying to fight it.
    I understood your point but when work is done in both main form and sub form, then undo command does not work because as soon as the user goes from main form to sub form, the data of the main form is saved in the table. which cannot be undo.

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    You can issue an Undo in a form event like LostFocus or Deactivate?
    Google for form events sequence.
    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

  5. #5
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    This makes no sense to me.

    If you know you are opening the form on what should be a locked record (which by your own description you do), then simply disable edits, or lock all the controls you don't want them to edit.
    You can simply unlock those controls, if they open the form to add a new record, or click on a command button to add a new record. As soon as that new record is saved you relock everything , if that's what is supposed to happen.

    Allowing the changes in the first place is causing all your difficulties, so don't allow them!
    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 ↓↓

  6. #6
    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
    This makes no sense to me.

    If you know you are opening the form on what should be a locked record (which by your own description you do), then simply disable edits, or lock all the controls you don't want them to edit.
    You can simply unlock those controls, if they open the form to add a new record, or click on a command button to add a new record. As soon as that new record is saved you relock everything , if that's what is supposed to happen.

    Allowing the changes in the first place is causing all your difficulties, so don't allow them!
    I agree with you, if the user has to add a new record, then he has to click on the Add Record button, which will unlock all the locks, then he can easily add new data, but if he closes the form while entering the data. then the record will be auto saved at that time, how to stop it?

  7. #7
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Quote Originally Posted by deepaksharma View Post
    I agree with you, if the user has to add a new record, then he has to click on the Add Record button, which will unlock all the locks, then he can easily add new data, but if he closes the form while entering the data. then the record will be auto saved at that time, how to stop it?
    It is up to you to validate what has been entered and what is required and what is not.
    You use the BeforeUpdate event of the form for that. It has a cancel option.

    Watch these videos.
    https://www.access-programmers.co.uk...-and-2.324342/
    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

  8. #8
    deepaksharma is offline Competent Performer
    Windows 7 32bit Access 2016
    Join Date
    Jul 2023
    Location
    india
    Posts
    389
    Quote Originally Posted by Welshgasman View Post
    It is up to you to validate what has been entered and what is required and what is not.
    You use the BeforeUpdate event of the form for that. It has a cancel option.

    Watch these videos.
    https://www.access-programmers.co.uk...-and-2.324342/
    You are also right, I had also tried making a program in which the text boxes of the main form were placed in the validation rules before update event, but the problem came when the user went to the sub form, the data of the main form was save without filling the sub form. It used to be auto saved.

  9. #9
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    The Before Update Form event will capture that as it will attempt to save when you move to the subform and fire.

    Just because you have cancelled the save, doesn't mean you can't put up a suitable message telling the user what they must fill out before using the sub form.
    If they then fill it out, then the subform will become available, as the before update event will "let them through" by saving the main forms data.

    If it is a new record, you can't enter data on the subform without saving the parent record anyway, as there would be no Primary key field data to save it against.

    If it is an existing record then it should already have the key data present.
    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 ↓↓

  10. #10
    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
    The Before Update Form event will capture that as it will attempt to save when you move to the subform and fire.

    Just because you have cancelled the save, doesn't mean you can't put up a suitable message telling the user what they must fill out before using the sub form.
    If they then fill it out, then the subform will become available, as the before update event will "let them through" by saving the main forms data.

    If it is a new record, you can't enter data on the subform without saving the parent record anyway, as there would be no Primary key field data to save it against.

    If it is an existing record then it should already have the key data present.
    I have put this coding in the before update event. while feeding a new record, if the user closes the form without filling the txt2 field, then it asks the user to answer yes or no. If the user answers in no, then on the txt2 field Instead of being directly focused. this message is being shown.
    Where am I going wrong?

    Private Sub Form_BeforeUpdate(Cancel As Integer)

    Set DB = CurrentDb
    Set RST = DB.OpenRecordset("GREY_OFFER")

    If (IsNull(Me![txt2].Value)) Or (Me.txt2.Value = "") Then

    Dim answer As Integer
    answer = MsgBox("Do you Wish to exit program without save data?", vbQuestion + vbYesNo + vbDefaultButton1, "User Repsonse")
    If answer = vbNo Then

    Me.txt2.SetFocus
    Cancel = True

    Else

    DoCmd.RunCommand acCmdUndo

    Exit Sub
    End If
    End If

    Me.OfferNumber = Nz(DMax("offernumber", "grey_offer"), 0) + 1
    End Sub
    Attached Thumbnails Attached Thumbnails msgUntitled.png  

  11. #11
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    Firstly, you are opening a recordset for no reason, you never use it.

    What message are you seeing, you haven't attached anything?

    I've indented your code, and removed the recordset, so you can better see what's happening, and used code tags.
    Code:
        
        Dim answer As Integer
    
    
        If IsNull(Me.[txt2]) Or Me.txt2 & "" = "" Then
            answer = MsgBox("Do you Wish to exit program without save data?", vbQuestion + vbYesNo + vbDefaultButton1, "User Repsonse")
            If answer = vbNo Then
                Me.txt2.SetFocus
                Cancel = True
            Else
                DoCmd.RunCommand acCmdUndo
                Exit Sub
            End If
        End If
    
    
        Me.OfferNumber = Nz(DMax("offernumber", "grey_offer"), 0) + 1
    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
    Quote Originally Posted by Minty View Post
    Firstly, you are opening a recordset for no reason, you never use it.

    What message are you seeing, you haven't attached anything?

    I've indented your code, and removed the recordset, so you can better see what's happening, and used code tags.
    Code:
        
        Dim answer As Integer
    
    
        If IsNull(Me.[txt2]) Or Me.txt2 & "" = "" Then
            answer = MsgBox("Do you Wish to exit program without save data?", vbQuestion + vbYesNo + vbDefaultButton1, "User Repsonse")
            If answer = vbNo Then
                Me.txt2.SetFocus
                Cancel = True
            Else
                DoCmd.RunCommand acCmdUndo
                Exit Sub
            End If
        End If
    
    
        Me.OfferNumber = Nz(DMax("offernumber", "grey_offer"), 0) + 1
    same message is coming even after deleting the record set.
    Attached Thumbnails Attached Thumbnails msgUntitled.png  

  13. #13
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    This code is on the Main form Before update event correct?
    And the record isn't already saved by some other method?

    I would probably have used

    Me.Undo rather than DoCmd.RunCommand acCmdUndo

    as it ensures you are referring to the forms current record.

    You might have to post up the database for us to see the issue.

    EDIT : have you tried just undoing the record and closing the form:
    Code:
        Dim answer As Integer
    
    
        If IsNull(Me.[txt2]) Or Me.txt2 & "" = "" Then
            answer = MsgBox("Do you Wish to exit program without save data?", vbQuestion + vbYesNo + vbDefaultButton1, "User Repsonse")
            If answer = vbNo Then
                Me.txt2.SetFocus
                Cancel = True
            Else
                Me.Undo
                DoCmd.Close acForm, Me.Name
                Exit Sub
            End If
        End If
    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
    Quote Originally Posted by Minty View Post
    This code is on the Main form Before update event correct?
    And the record isn't already saved by some other method?

    I would probably have used

    Me.Undo rather than DoCmd.RunCommand acCmdUndo

    as it ensures you are referring to the forms current record.

    You might have to post up the database for us to see the issue.

    EDIT : have you tried just undoing the record and closing the form:
    Code:
        Dim answer As Integer
    
    
        If IsNull(Me.[txt2]) Or Me.txt2 & "" = "" Then
            answer = MsgBox("Do you Wish to exit program without save data?", vbQuestion + vbYesNo + vbDefaultButton1, "User Repsonse")
            If answer = vbNo Then
                Me.txt2.SetFocus
                Cancel = True
            Else
                Me.Undo
                DoCmd.Close acForm, Me.Name
                Exit Sub
            End If
        End If
    All the points mentioned by you are correct, still while closing the form (during offer entry) an error box is coming on selecting "no". please check the attachment.....
    Attached Files Attached Files

  15. #15
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    The problem is that you are setting the offer date in code, which instantly dirties the record.
    There are a host of other things that aren't correct but that is causing most of your issues. Just put in Date() in the default value of the control, that doesn't dirty the record when you go to a new one.

    Also, please please please give your controls meaningful names. txt1, txt 2 txt133 mean absolutely nothing, and make debugging a nightmare.
    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 ↓↓

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

Similar Threads

  1. control undo
    By markjkubicki in forum Programming
    Replies: 3
    Last Post: 08-18-2021, 12:43 AM
  2. Replies: 4
    Last Post: 11-24-2020, 03:41 PM
  3. to Undo, or not to Undo...
    By Davince in forum Forms
    Replies: 2
    Last Post: 02-19-2015, 07:23 PM
  4. Replies: 2
    Last Post: 10-31-2012, 11:52 AM
  5. Undo problem
    By Waubain in forum Programming
    Replies: 4
    Last Post: 10-27-2011, 12:36 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