Results 1 to 8 of 8
  1. #1
    cherold is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2014
    Posts
    82

    How would I update 2 tables based on an edit in a subform?

    I have a subform that contains a table (in datasheet form). If I add a new record to the table from within the subform, I need the table to automatically enter data in a field and also need to add a record to a second table. I'm not sure how to make that all happen (or even if this question is more properly about forms or queries or something else).

    There are three relevant tables (these are used to catalog my album collection, btw):

    LPs
    lps_id, album_title, artist_id

    Songs
    songs_id, song_title, artist_id

    LPContents
    lpcontents_id, song_id, lp_id

    The main form displays a single album.

    The subform displays all songs contained in that album (as indicated by LPContents).

    What I need to do is: If I add a new song record in the subform, I need Songs.artist_id to be set to LPs.artist_id for the current album, and I need LPContents to have a new record with LPs.lps_id and the just-added songs_id.

    I would think I would want to trigger these updates on an event, but none of the events look like what I need, which would be called something like OnRecordAdded.



    I could write something in VBA to do the updates if I could figure out a way to trigger it when a song is added, although if there's some simpler way than VBA I'd like to hear about it.


    I hope that's clear. Thanks.

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    What is the purpose of the contents table? The lp_id in the songs table is enough to display all songs on an album.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    cherold is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2014
    Posts
    82
    Apologies, I wrote that wrong. The Songs Table doesn't have an lp_id, it has an artist_id. I'll edit my question. Thanks.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Subform is bound to LPContents and you select songs from a combobox that pulls list from Songs? You want to add a new song 'on-the-fly' while doing data entry? Use combobox NotInList event. This is a common topic. Review https://www.accessforums.net/showthread.php?t=70996
    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
    cherold is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2014
    Posts
    82
    It's not a combobox, it's a text field. User types in the name of the song. I could call an event when the field is changed, but that means everytime I edit the title of an existing song in any way it would trigger the event, when I only need to know if a new record has been created.

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    I'd probably have the lp id in the song table, but you could use the after update event of the form with a test:

    If Me.NewRecord Then
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Why not use a combobox? User can still type into the box.
    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.

  8. #8
    cherold is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2014
    Posts
    82
    I actually discovered that there is an AfterInsert event. I guess before I was looking at events for individual fields instead of the subform as a whole. So I call a script on that.

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

Similar Threads

  1. Replies: 3
    Last Post: 12-08-2017, 09:20 AM
  2. Replies: 2
    Last Post: 08-07-2015, 02:11 AM
  3. Replies: 3
    Last Post: 04-08-2014, 07:08 AM
  4. Replies: 3
    Last Post: 02-10-2010, 07:29 AM
  5. Replies: 3
    Last Post: 08-06-2009, 11:49 PM

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