Results 1 to 7 of 7
  1. #1
    PeakH is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2017
    Posts
    143

    How to create new Main Form record when adding Subform record first

    I have a main form with a couple sub-forms that all work great, except when I add a record to the sub-form before adding anything to the main form, it does not create a new record in the main form. I think I understand why this happens but I would like to change it.
    I would like to make it so that adding a record on the sub-form will create a new record on the main form just as if you had added information to the main form (Not a new main form record for each sub-form record, just a single new main form record whenever you start typing anywhere on an empty form).



    Please let me know if I need to elaborate.

    Any suggestions?

  2. #2
    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,725
    I'm not sure I understand the requirement.
    Form/subform are usually used when tables have a 1 to many relationship between them.
    eg. A Parent has 0,1 or ManyChildren

    Person ---> Child

    You can not add/create a Child record before you have the related Parent.
    You add the Parent record, then add the Child(s).

    Generally,if the parent exists on the MainForm, you should be able to add 1 or more Child records.

  3. #3
    PeakH is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2017
    Posts
    143
    Right, which is exactly why when I forget and start typing in the subform first and then click onto the main form, the subform record disappears, since the subform filters based on the parent record.
    What I would like to know is if there is any way that creating a child record can prompt the creation of its parent record. That way, when someone is using the front-end, they don't have to constantly remember to add the information in the main form first and instead can just start typing anywhere on the form.

  4. #4
    Micron is offline Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,800
    The thing is, what field are the two linked by, and what would you put into the parent record on the main form? Maybe don't show the subform when the main loads until the necessary value is entered into the main. I suppose you could use the default value property of the table fields that the forms are linked on, assuming they don't have to be unique values. The only other possibilities I can think of at the moment is to employ temp tables to get around any main table index issues, or open the form with a default value (such as the user login name) in the main and subform linked controls. You'd check this value before allowing the record to update, and if it remains, cancel the update. The edit of this main form control would trigger the linked subform control to adopt the same value.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  5. #5
    PeakH is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2017
    Posts
    143
    Hmm... I see. They are linked on unique values. It's an order confirmation form with a subform of order details, so they are linked on the order confimation auto number.
    I hadn't thought about hiding the subforms until there is a parent record. I could try including an "add details" button to allow the user access to the subforms, but only if there is a parent record. Or something like that.

    Thanks for the idea!

  6. #6
    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
    As orange said, you cannot create a Child Record until you have a Parent Record. So if a user tries to do that, simply warn them that this can't be done and return Focus to the Main Form proper. You say that the order confirmation number is an Autonumber, so it won't be populated until you've at least started to enter a New Record, so if you're on a New Record in the Main Form, and the order confirmation number is Null, do just that:

    Where

    1. SubFormControlName is the name of the Subform Control (which may or may not be the same as the Form the Subform is based on)
    2. OrderConfirmationNumber is just that (replace with your actual name of your Autonumber)
    3. AnyOtherControl is just that, the name of any other Control on the Main Form, say, for instance, the one holding the customer's name)

    use this code:

    Code:
    Private Sub SubFormControlName_Enter()
     If Me.NewRecord And IsNull(Me.OrderConfirmationNumber) Then
       MsgBox "A New Main Order Form Record Must be Entered First!"
       Me.AnyOtherControl.SetFocus
     End If
    End Sub

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

    All posts/responses based on Access 2003/2007

  7. #7
    PeakH is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2017
    Posts
    143
    This works perfectly! And a quick fix.
    Thanks for all your help.

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

Similar Threads

  1. Replies: 10
    Last Post: 12-17-2014, 03:31 PM
  2. Replies: 8
    Last Post: 11-26-2013, 12:21 PM
  3. Replies: 6
    Last Post: 08-22-2012, 03:24 AM
  4. Replies: 4
    Last Post: 03-14-2012, 10:08 AM
  5. Replies: 2
    Last Post: 12-07-2011, 02:51 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