Results 1 to 15 of 15
  1. #1
    MatthewGrace is offline Competent Performer
    Windows 8 Access 2010 64bit
    Join Date
    Jan 2013
    Posts
    159

    Entering Subform Records when the main forms PK is (new)

    I have what I believe to be a very common issue, and I can solve it a few ways, but I'd like to know what practice people think is best. One and Many table are represented on the Form/Subform below. Since the subform's master/child fields are properly established, Access knows to automatically fill in the FK into the Many table. But in the picture below, you can see a potential stumbling block: if the User jumps to a new record, and starts to fill in the subform while skipping the main form, there is no way for Access to link these two. You see me entering many records here, and they're all orphaned by default:

    Click image for larger version. 

Name:	Untitled-1.jpg 
Views:	27 
Size:	82.0 KB 
ID:	29362

    What is the desirable way to turn off or disallow the User from adding records into a "many" subform like you see above, until the main "one" form to which it's master/child fields are linked, has it's Primary Key established?

    PS: I've used the relationships pane to Enforce Referential Integrity, and shockingly, that doesn't prevent Access from orphaning all the records in the above picture!

  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,543
    With TableOneFK field, change the "Required" property to yes and remove the 0 from its' "Default Value" property.
    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
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    Save the main rec. then enter all sub recs.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,919
    Could use VBA to disable the subform container until data entered into a field of the main form. Use the main form OnCurrent event as well as textbox AfterUpdate event.
    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.

  5. #5
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,411
    I should think that referential integrity enforcement via the relationship diagram should take care of this and make it impossible to add the many unless the one exists.

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,919
    Did some testing. Referential integrity alone does not prevent creating orphans.

    What it can do, if CascadeDelete is activated, is delete dependent records if a parent record is deleted. For example, delete an order from Orders table then dependent records in OrderDetails would be deleted. And CascadeUpdate would cascade an edit of PK value (when a PK is not an autonumber field) to the FK field in dependent table.
    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.

  7. #7
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,411
    Attachment 29372 Attachment 29373

    Here's how I do it. You cannot add a Book on the Many side if there is no Author on the One side; Access won't allow it.

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,919
    Ooops, okay, tested more and got that to work. I so seldom rely on the Relationships settings to manage data integrity, lost track of that functionality.

    However, I would still recommend the code so users don't get annoyed by popups. Don't even let them get into the subform, absentmindedly or otherwise, until appropriate condition met.
    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.

  9. #9
    MatthewGrace is offline Competent Performer
    Windows 8 Access 2010 64bit
    Join Date
    Jan 2013
    Posts
    159
    @Davegri / @June

    Quote Originally Posted by davegri View Post

    Here's how I do it. You cannot add a Book on the Many side if there is no Author on the One side; Access won't allow it.
    Surprisingly Access does allow it. As I said in OP, I've used the relationships pane to Enforce Referential Integrity, and shockingly, that doesn't prevent Access from orphaning all the records:

    Click image for larger version. 

Name:	Untitled-2.jpg 
Views:	16 
Size:	122.0 KB 
ID:	29380

    Yet in Dave's example it appears to work beautifully. I think what I'm learning is that the ERI option in the relationships pane doesn't stop Null values from being placed into a foreign key field! My gut would have told me for sure Access wouldn't allow that. I think what Dave has going for him is that his FK control (textbox/combobox) is set to default to 0, and that somehow triggers Access's built-in ERI to say "you have to have a related record in the 'one' table". In other words, ERI doesn't trigger for Null values, it only works when a real integer is stored into the cell.

    When I built this form - don't laugh - I used to Form Wizard. I guess the wizard did not set that up correctly to stop orphaned records.

    I don't suppose either of you guys know how to modify the MessageBox that popped up in Davegri's example titled "Patti's Library"? As always, I appreciate all your efforts.

  10. #10
    MatthewGrace is offline Competent Performer
    Windows 8 Access 2010 64bit
    Join Date
    Jan 2013
    Posts
    159
    Yea, the more I toy with this, the more I realize because of the various popups that use language not friendly to end users, it's best in my situation to do it all VBA per June's suggestion. Unless someone knows how to override those popups with a friendlier message, which would be cool. Thank you all.

  11. #11
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,411
    Add this to the subform code:
    Code:
    Private Sub Form_BeforeUpdate(Cancel As Integer)
        If Me.Parent.NewRecord Then
            MsgBox "Unable to add a record until a record exists in the main form", vbCritical + vbOKOnly, "  N E E D   I N P U T   "
            Cancel = True
            Me.Undo
        End If
    End Sub
    It checks to see if a record exists yet in the main form.
    And yes, I'm amazed that Access allows orphan records in that manner!

  12. #12
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,919
    Yes, a friendly message but still a popup user has to respond to. I try to design and program to prevent popups that frustrate users and reduce productivity, hence preventing access to subform unless conditions are appropriate.
    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.

  13. #13
    andrejamih is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Dec 2017
    Posts
    15
    Hello! I have the same problem with my main form and few tabbed subforms. What would be the VBA code for main form OnCurrent event and on textbox AfterUpdate event for preventing adding data into subofrms' fields without filling Master field on main form which is also a PK?

  14. #14
    Join Date
    Apr 2017
    Posts
    1,679
    Don't use default values on tables! When you need a default value, define it for form control instead!

  15. #15
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,919
    @andrejamih, should start a new thread with your question instead of hijacking an old one. A question will get more attention as a new one with no replies.

    Code to manage availability subform container could be like:

    Me.ctrSubform.Enabled = Not Me.NewRecord

    or

    Me.ctrSubform.Enabled = Not IsNull(Me.Customer)
    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.

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

Similar Threads

  1. Replies: 2
    Last Post: 08-05-2014, 07:06 AM
  2. Replies: 1
    Last Post: 07-18-2014, 04:11 AM
  3. Replies: 6
    Last Post: 05-10-2014, 05:11 PM
  4. Changing records on Main and Subform
    By steve9495 in forum Forms
    Replies: 10
    Last Post: 07-14-2012, 01:46 PM
  5. Replies: 3
    Last Post: 04-17-2012, 10:28 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