Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    diegomarino is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2018
    Posts
    409

    close form without save

    hi,
    i have a button in my form that i use to close the form without saving



    Code:
       DoCmd.Close acForm, "myform", acSaveNo
    the problem is that i have a subform in it that even if i use the method below saves the recors.

    how can i close my form without saving the records in subforms (i add throught vba some records in open event)?

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    data save is automatic.
    the ACSAVENO is for form changes, NOT data changes.

  3. #3
    diegomarino is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2018
    Posts
    409
    so the only way is to run a delete query in case of new record?

  4. #4
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    You can use the BeforeUpdate event of the subform to stop the new record (usually with Me.Undo).
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  5. #5
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,800
    Creating records via code then wanting to delete them is not a good approach. If there is a reason that the records are not wanted, why not figure that out before you add them, and then don't add them? There are only 3 ways that I know of that you can 'create' a record and not save it:
    - do validation before moving off or saving the record and use Me.Undo and Me.Cancel in the form BeforeUpdate event
    - use an unbound form, but this requires code and any other method is likely easier to implement and maintain
    - use a form bound to a staging table (one that mimics the main table but has no PK field). If you want to keep the records, copy to the main table then delete from the staging table. If your db is not split and each user has their own front end (not advisable), this is not practical as users can interfere with each other's records.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  6. #6
    diegomarino is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2018
    Posts
    409
    me.undo is a good idea.

    i was thinking to use BeforeInsert event to add those records in the subform too.
    maybe i make some tests and i let you know.

  7. #7
    diegomarino is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2018
    Posts
    409
    sorry, since the form i have to me.undo is the subform linked, i was not able to use properly that method.
    how should i put the me.undo? in the subform beforeupdate event? and what's the trigger to start the me.undo?

  8. #8
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,800
    Post your code attempts. "I wasn't able to..." tells us nothing. Your form reference might have been wrong. Hopefully such things would be discoverable, but certainly not if you don't show the attempt. Validation code for subform records should be on/behind the subform, so Me.Undo and Me.Cancel would go there if the validation fails. If you are also wanting to delete a main form record that is the parent record for these subform records, then that is a different thing. If that is the case then IMO, you'll have to forget about undoing subform records. Instead, you would delete the main form record (cannot be "undone" because it would already be saved). If you have created a relationship between these tables and have set Cascade delete option, deleting the main record will delete the child records as well.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  9. #9
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    Diego,
    You would need to explain a bit better your scenario. In a form\subform setup you would save the main record - no need for you to do anything, Access will do it when you move the focus from the main form to the subform. Now that you are in the subform and edit the new record the subform's instance becomes "Dirty" meaning it is in edit mode. To cancel that you need to add code to the BeforeUpdate of the subform:

    Code:
    If SomeCondition = True 'your reason why you want to cancel the update
           Me.Undo
           Cancel=True  'notice it is not Me.Cancel but the Cancel setting of the BeforeUpdate event
    End If
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  10. #10
    diegomarino is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2018
    Posts
    409
    i found the sick part

    on open event of man form i have this code

    Code:
      If Me.NewRecord Then      
          If Me.OpenArgs <> "" Then
             With Me.Partecipanti
                .SetFocus
                .Form.AllowAdditions = True
                DoCmd.RunCommand acCmdRecordsGoToNew
                .Form![PartecipanteID].SetFocus
                .Form!PartecipanteID = 793
                .Form!Qualifica = "Head Hunter"
                DoCmd.RunCommand acCmdRecordsGoToNew
                If InStr(Me.OpenArgs, "\") Then
                   .Form!PartecipanteID = Left(Me.OpenArgs, InStr(Me.OpenArgs, "\") - 1)
                Else
                   .Form!PartecipanteID = Me.OpenArgs
                End If
                .Form!Qualifica = "Candidato"
             End With
          Else
             With Me.Partecipanti
                .SetFocus
                .Form.AllowAdditions = True
                DoCmd.RunCommand acCmdRecordsGoToNew
                .Form![PartecipanteID].SetFocus
                .Form!PartecipanteID = 793
             End With
          End If
       End If
    (i just polished parts that are just cosmetic or do not make anything related to the issue)

    as soon the form opens the records in subform are saved in sharepoint.
    i'm wondering if there is a way to just set values in subform without saving it in sharepoint

    this is my beforeUpdate event of the subform

    Code:
    Private Sub Form_BeforeUpdate(Cancel As Integer)   If SaveRcd = True Then
          Me.Undo
          Cancel = True
       End If
    End Sub
    SaveRcd is a global variable that is set to true when i click the button i use to exit the form wothout saving

  11. #11
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,800
    i'm wondering if there is a way to just set values in subform without saving it in sharepoint
    I believe I gave you 2 methods in post 5. Unbound form or staging table.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  12. #12
    diegomarino is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2018
    Posts
    409
    ok, it just looked strange that i could not make it in and easier way.
    so unbound subform will be. thanks

  13. #13
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    You seem to be adding two new records so I don't think the unbound form will work. What is the business reason to create the subform records on open of the main form and delete them sometimes but not always?
    Where do you set the value of SaveRcd? Why not use that in the main form's open event to avoid creating the records if SaveRcd = True (one extra if statement after If.NewRecord).

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

  14. #14
    diegomarino is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2018
    Posts
    409
    1) ok, i'll try the unbond form to see if it works, if not i'll try a couple of ideas that i had
    2) this form is the calendar detail, the only problem is when i create a new record and then i do not want to save cause i made it wrong; the subform are the partecipants to the calendar, it's very important to me cause i have linked mail that i use to send teams invitation automatically, that spare me a lot of time
    3) i set SaveRcd to false on main form open event, and i set it to true when i click on the button that i use to cancel the new calendar
    4) I can't use it on form open event, cause if save the new calendar or not it's a decision i take in a second moment

    I was thinking to those solution, if an unbound form will not work.

    1) make a delete query if i cancel the new calendar, so i can delete the records the subform saves at the form opening
    2) add the records in the subform on the BeforeUpdate event, if savercd = false

  15. #15
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,800
    If you write these "temporary" records to a staging table(s) you can simply delete from there and never affect your "main" records. That is especially useful if your current process of deleting would cause gaps in sequential values such as ticket/work order or some other kind of sequential value.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

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

Similar Threads

  1. Can't save and close form
    By virgilio in forum Access
    Replies: 3
    Last Post: 06-04-2020, 01:23 PM
  2. Close form and dont save record
    By Homegrownandy in forum Access
    Replies: 10
    Last Post: 08-05-2015, 03:47 AM
  3. Can't save or close form
    By Buakaw in forum Forms
    Replies: 4
    Last Post: 07-26-2011, 04:47 AM
  4. form won't save and close
    By Philislost in forum Access
    Replies: 6
    Last Post: 10-08-2010, 01:47 PM
  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