Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    matey56 is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Jul 2020
    Posts
    188

    Help - Save button and a Notification to Save a Change (subform)

    Hi all,
    I have followed the instructions in the link below to add a save button (that is grayed out until data is entered) and a notification when the form is closed without saving first. It works perfectly, but I have several forms that have subforms that it doesn't work with. The save button is on the main form and the records are on the subform. How can I tweak the code to make it work in these situations?

    https://www.iaccessworld.com/create-...n-save-change/

    Here is the vba code I added:

    Private Sub Form_Dirty(Cancel As Integer)
    Me.cmdSave.Enabled = True
    End Sub

    Private Sub cmdSave_Click()
    Saved = True
    DoCmd.RunCommand(acCmdSaveRecord)
    Me.cmdSave.Enabled = False
    Saved = False
    End Sub

    Private Sub Form_BeforeUpdate(Cancel As Integer)
    Dim Response As Integer
    If Saved = False Then


    Response = MsgBox("Do you want to save the changes on this record?", vbYesNo, "Save Changes?")
    If Response = vbNo Then
    Me.Undo
    End If
    Me.cmdSave.Enabled = False
    End If
    End Sub

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    Why the save button?
    Data is saved automatically when user leaves the record.

  3. #3
    Join Date
    May 2018
    Location
    Living in Scotland UK
    Posts
    1,821
    I agree with ranman, there is no need for any of the code just have a Close Button

  4. #4
    matey56 is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Jul 2020
    Posts
    188
    I knew someone was going to ask me that. Users were messing up the data too often. They would often times type incorrect data then close the tab thinking the data wasn't saved. Because that's how Word, Excel, etc. works. They're just used to it.

    Quote Originally Posted by ranman256 View Post
    Why the save button?
    Data is saved automatically when user leaves the record.

  5. #5
    matey56 is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Jul 2020
    Posts
    188
    So is there a way to make this work using the form/subform relationship?

  6. #6
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    I believe you would need to duplicate the code in the subforms?, as soon as they move to the mainform their data would be saved.
    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

  7. #7
    matey56 is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Jul 2020
    Posts
    188
    Quote Originally Posted by Welshgasman View Post
    I believe you would need to duplicate the code in the subforms?, as soon as they move to the mainform their data would be saved.
    I did that, but it's not working. I just don't know what's missing.

  8. #8
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,250
    Have a look at this related thread and download the sample I uploaded there and review it.
    https://www.access-programmers.co.uk.../#post-1863514

    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  9. #9
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,250
    Edit: double-post
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  10. #10
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    Ad Vlad's code demonstrates, use the Cancel argument of BeforeInsert and BeforeUpdate events.

    Could also have data validation for each control.

    Data entry into a database is not like creating a Word or Excel document. In my experience, users expect data to be saved when they click a Save button and don't need to be annoyed with a message to verify. That's why I provide a Cancel button as well as a Save button.

    I have never used form BeforeUpdate or BeforeInsert events.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  11. #11
    matey56 is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Jul 2020
    Posts
    188
    I keep getting errors because the subform doesn't have the cmdSave button.

  12. #12
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,250
    And what are the errors please? Have you look at the sample?

  13. #13
    matey56 is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Jul 2020
    Posts
    188
    Quote Originally Posted by Gicu View Post
    And what are the errors please? Have you look at the sample?
    Here's one for example:

    Click image for larger version. 

Name:	Capture.JPG 
Views:	6 
Size:	29.7 KB 
ID:	49841

    It's because he cmdSave button is not in the subform right? I'm using an unbound box in my main form to retrieve records in my subform.

  14. #14
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,250
    Of course that is the cause, and it looks like you have a reference to the Save command button in the previous event as well. You need to modify the code to match your forms. Not sure what an unbound box is, I suspect you use it as the Master field in the Master\Child linking fields. Is the main form unbound? If yes what is to save there? You need to show us a bit more or spend the time to understand what the code does and modify it to work for the subform.
    I am attaching the sample I mentioned in my original post, review how the code is implemented in the subform.

    Cheers,
    Attached Files Attached Files
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  15. #15
    matey56 is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Jul 2020
    Posts
    188
    The main form is not unbound. It's record source is a query that displays data from a separate table. However, there is nothing to save on the main form. It's display data only (thus the reason for the subform). It's just the only space for a save button because it's a one-to-many relationship and each product will have dozens of records displayed on the subform. I don't want each record to have a save button. Once the user selects a product from the unbound box on the main form, it requeries the subform (see code below). If the save button is not on the subform, I don't know how to make the code work.


    Private Sub cbo_select_product_AfterUpdate()
    Me.Requery
    Me.qsfm_Label_Tracking.Form.Requery

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

Similar Threads

  1. Replies: 2
    Last Post: 11-13-2017, 08:51 AM
  2. Replies: 15
    Last Post: 01-28-2014, 12:20 PM
  3. form - save entered data only with save button
    By cbrxxrider in forum Forms
    Replies: 3
    Last Post: 10-20-2013, 12:39 PM
  4. Replies: 5
    Last Post: 12-19-2012, 01:05 PM
  5. Replies: 3
    Last Post: 10-08-2012, 08:40 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