Results 1 to 8 of 8
  1. #1
    bilalo is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2019
    Posts
    107

    form with empty sub form

    I have a form and a sub form. I want to prevent saving records in the form if the sub form is empty.


    My current solution is the form_close event, I check the RecordsetClone.RecordCount of the subform, if it is equal to zero then I delete the record on the form using a DoCmd.RunSQL command.
    It is working most of the time, but I don't know why I am still finding (rarely) some records with empty sub form.
    What could be the problem? Is there a better solution?

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    So you only want a parent record if there are dependent records? Need to provide code for analysis.
    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.

  3. #3
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    When you edit a main form record and move to the subform, you've just committed the record change in the main.
    When you close a form, the subform closes first, so where is DoCmd.RunSQL? If on the main, too late. If you post the code, both close events might be needed for review.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  4. #4
    bilalo is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2019
    Posts
    107
    This is the code I am using:

    Private Sub Form_Close()
    If Nz(Me.frmSubOrder.Form.RecordsetClone.RecordCount, 0) = 0 Then
    DoCmd.RunSQL "Delete from Orders where OrderNo=" & OrderNo
    End If
    End Sub

    Does it make a difference if I use this code on the subform_close() instead of form_close() ?

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    I just did a test.

    Main form Close event runs before subform Close.

    Can you identify conditions when it doesn't work? Have you step debugged?

    If you want to try in the subform:

    If Nz(Me.RecordsetClone.RecordCount, 0) = 0 Then
    DoCmd.RunSQL "Delete from Orders where OrderNo=" & Me.Parent.OrderNo
    End If
    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.

  6. #6
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    Main form Close event runs before subform Close.
    Sorry, I meant to say unload. Subform unloads first. I suspect a record save occurs in the unload event, not the close event, which is why I tested that event. I reported on the close event by mistake. You could try moving code to unload event as I think the record is potentially saved before close event.

  7. #7
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    bilalo,

    Can you tell us more about the data involved? Usually form/subform construct represents tables in a 1 to M relationship. eg an Order has 1 or Many Items; a Project has 1 or Many Tasks...
    Seems your situation is if I don't have child records, don't record anything???
    Just looking for a little clarification.

  8. #8
    bilalo is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2019
    Posts
    107
    Quote Originally Posted by orange View Post
    bilalo,

    Can you tell us more about the data involved? Usually form/subform construct represents tables in a 1 to M relationship. eg an Order has 1 or Many Items; a Project has 1 or Many Tasks...
    Seems your situation is if I don't have child records, don't record anything???
    Just looking for a little clarification.
    Yes that is exactly my case, I need the order to be cancelled if it doesn't contain any item

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

Similar Threads

  1. Form empty by defeault
    By miicker in forum Forms
    Replies: 1
    Last Post: 02-26-2013, 10:04 AM
  2. loading empty form
    By Suzie2012 in forum Forms
    Replies: 5
    Last Post: 10-10-2012, 07:03 PM
  3. No results w/empty form
    By Bruce in forum Queries
    Replies: 9
    Last Post: 05-06-2012, 09:52 AM
  4. Replies: 4
    Last Post: 11-20-2011, 01:08 PM
  5. Populate or Empty Form
    By gazzieh in forum Forms
    Replies: 0
    Last Post: 12-02-2009, 05:56 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