Results 1 to 12 of 12
  1. #1
    hemanaguib is offline Novice
    Windows 10 Access 2013 64bit
    Join Date
    Jun 2017
    Posts
    8

    Red face How to stop saving main form untill filling subform

    Hi All,



    As mentioned in title, I want not to save the main form if I do not enter the data in the subform?

    I tried many methods and search without luck

    I hope if some one can help me here;

    Thanks.

  2. #2
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 7 32bit Access 2013
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,540
    I think you would need to run some code in the main forms Close or Unload event to check for the existence of data in the subform and then delete the main form record if none is found.
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  3. #3
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Then how could you ever enter data in the Subform? The Main Form Record would always be deleted, because a Main Form Record has to exist before entering a Subform Record.

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  4. #4
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,412
    only way I can think would work would be to use unbound main and subforms then use vba transaction coding (begintrans/committrans) to ensure mainform data is inserted, get the record identify, add it to the subform family key and append those records

  5. #5
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    I believe the technical term for that is a mare's nest!

    The question this begs...actually two questions is...

    Why would you enter a Main Form Record if you didn't have data at hand for a Subform Record?

    And if you simply had to enter the Main Form Record...and discover you don't have data at hand for a Subform Record...why not simply delete the Main Form Record?

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  6. #6
    hemanaguib is offline Novice
    Windows 10 Access 2013 64bit
    Join Date
    Jun 2017
    Posts
    8
    Quote Originally Posted by Bob Fitz View Post
    I think you would need to run some code in the main forms Close or Unload event to check for the existence of data in the subform and then delete the main form record if none is found.
    Thanks for the reply,
    Do you have any sample of that code?

  7. #7
    hemanaguib is offline Novice
    Windows 10 Access 2013 64bit
    Join Date
    Jun 2017
    Posts
    8
    Quote Originally Posted by Missinglinq View Post
    I believe the technical term for that is a mare's nest!

    The question this begs...actually two questions is...

    Why would you enter a Main Form Record if you didn't have data at hand for a Subform Record?

    And if you simply had to enter the Main Form Record...and discover you don't have data at hand for a Subform Record...why not simply delete the Main Form Record?

    Linq ;0)>
    Thanks for the reply,

    The database are shared on network, where data entry colleagues enter by mistake the form and forget about subform. finally I have some many records nonsense. That is why I'm looking for a solution?

  8. #8
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Quote Originally Posted by hemanaguib View Post

    Do you have any sample of that code?
    Bob was, unusually for him, mistaken...you can't do that! Think about it...You have to have a Record, in the Main Form, before you enter a Record in the Subform. When you finish entering a new Record, in the Main Form, there will never be a Record in the Subform...there can't be.

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  9. #9
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 7 32bit Access 2013
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,540
    Quote Originally Posted by Missinglinq View Post
    Bob was, unusually for him, mistaken...you can't do that! Think about it...You have to have a Record, in the Main Form, before you enter a Record in the Subform. When you finish entering a new Record, in the Main Form, there will never be a Record in the Subform...there can't be.

    Linq ;0)>
    I know that there would never be a record in the sub form without a record in the main form. However, it is possible to create a record in the main form without creating one in the sub form which, as I understood it, is what the OP is trying to eliminate. My suggestion was for code in the main forms Close or Unload event to check for this eventuality and then delete the main form record if no sub form record exists. As I understand the order of events, neither of these events will fire until the user tries to close the main form. I was not suggesting that the code should be run when a user moves the focus from the main form to the sub form, if that is what you thought I meant. Obviously, that would never work as needed.
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  10. #10
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Sorry, Bob! I misunderstood!

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  11. #11
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 7 32bit Access 2013
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,540
    For the benefit of the OP I have attached a simple example of what I meant.
    Feel free to ask any questions you may have.
    Attached Files Attached Files
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  12. #12
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Following Bob's suggestion, here's another way that works:

    Code:
    Private Sub Form_Unload(Cancel As Integer)
     If DCount("*", "SubTableName", "MainID = " & Me.MainID) < 1 Then
       DoCmd.SetWarnings False
       DoCmd.RunCommand acCmdSelectRecord
       DoCmd.RunCommand acCmdDelete
       DoCmd.SetWarnings True
     End If
    End Sub
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

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

Similar Threads

  1. Replies: 4
    Last Post: 02-11-2016, 11:22 AM
  2. Replies: 4
    Last Post: 11-06-2014, 05:35 AM
  3. Replies: 5
    Last Post: 02-12-2014, 11:52 PM
  4. Replies: 3
    Last Post: 04-17-2012, 10:28 AM
  5. Replies: 12
    Last Post: 01-18-2012, 10:02 AM

Tags for this Thread

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