Results 1 to 5 of 5
  1. #1
    Pianopizza5 is offline Advanced Beginner
    Windows 10 Access 2013 32bit
    Join Date
    Sep 2019
    Posts
    46

    Use append query add items to subforms

    In my specific project, I want all the subform items to populate when a record is made in the master form (I don’t want the user to have to enter any information, other than the record in the master form). I tried using an append query triggered in the form’s On Load event. The append query adds the necessary information to the composite table, but the problem is that the “link field” from the main form needs to populate next to the appended items for the items to display. I tried passing the link field from the main form, but no luck. Any thoughts on how to do this?



    For example:

    Table 1: tblSports
    Table 2 (composite table): tblSportsStats
    Table 3: tblSchoolStudents

    The main form is Table 1 (Master field: SportID)
    The subform is Table 2 (Link field: SportID)
    With a lookup of the school student

    I want to be able to easily add the school students to the sport (and add more if any other students come) and add additional sports.

    My append query is: StudentID from the tblSchoolStudent to the StudentID field in the tblSportsStats
    I have the criteria of: Not In ([tblSportsStats]![StudentID])
    This prevents me from adding duplicates
    But this creates a problem because then I can’t use the query for adding the students when I have another sport (duplicate StudentIDs)

    Any ideas? I’m open to using VBA and any other approaches.

    Thanks.

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    First have to commit parent record to table. Then VBA can build and execute an INSERT SELECT action SQL to "batch" create a set of dependent records. This is a fairly common topic. Here is one example https://stackoverflow.com/questions/...-in-access-vba

    Your SQL code could be something like:
    Code:
    CurrentDb.Execute "INSERT INTO tblSportsStats(StudentID, SportID) SELECT StudentID, " & Me.cbxSport & " FROM tbSchoolStudents"
    Set StudentID and SportID as a compound index (no duplicates allowed) in tblSportsStats and WHERE criteria will not be needed.
    Last edited by June7; 11-08-2024 at 10:11 AM.
    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
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,879
    Here's an example where you can add a student 1 by 1 or add several at a time and avoid duplicates.
    Attached Files Attached Files
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  4. #4
    Pianopizza5 is offline Advanced Beginner
    Windows 10 Access 2013 32bit
    Join Date
    Sep 2019
    Posts
    46
    Hello,

    Thank you all so much! Your posts were very helpful and gave me some ideas. One step that I needed to do was reference the form field in my query. Then, I followed the video here (which performs a query and then compares those results to another query).

    https://www.youtube.com/watch?v=kflL...V737s&index=74

    Thank you! I got everything working beautifully.

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    Post 4 was moderated, I'm posting to trigger email notifications.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Replies: 3
    Last Post: 08-31-2020, 05:02 PM
  2. Replies: 11
    Last Post: 05-13-2015, 11:10 PM
  3. Subforms Inside Subforms
    By LordPanzer in forum Access
    Replies: 1
    Last Post: 10-11-2013, 05:19 PM
  4. Replies: 4
    Last Post: 06-17-2013, 12:00 PM
  5. Items With Which items Most Commonly
    By vansicklej in forum Access
    Replies: 3
    Last Post: 08-10-2011, 11:05 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