Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    msmithtlh is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Mar 2013
    Posts
    151

    Simultaneously add a record to 3 tables

    I’m working on a database for an event that takes place next week. Because there were so many data items for each person, I have split the main data into 3 tables:

    1. MAINPARTICIPANTS (primary key – autonumber “participantID”
    2. MAINSPORTS (primary key is an autonumber, secondary key is “participantID”
    3. MAINTRANSPORTATION (primary key is an autonumber, secondary key is “participantID.”

    Although there is probably a better way to structure the data, I don’t have time to do that. My problem is that when the user adds a person to MAINPARTICIPANTS, I need it to add a record to MAINSPORTS and MAINTRANSPORTATION (with the same participantID), but haven’t figured that out.

    Urgently need help!! Thanks!!!

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,522
    you dont do it simutaneously,
    but just run 3 append queries. 1 for each table.

  3. #3
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    You haven't shown how you're adding, but you'd need to get the ID from the first append to do the others. Note how Allen does it here:

    http://allenbrowne.com/ser-57.html
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  4. #4
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142
    How about a "datamacro" or table macro that runs on the tables After Insert event. Its as easy as after insert add new record to other table, set foreign key in other table equal to primary key in main table. Repeat.

  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,521
    I thought about that but couldn't remember if they were available in 2010. My gut said no so I didn't mention it.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    msmithtlh is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Mar 2013
    Posts
    151
    I need to clarify. I have 3 forms, 1 for MAINPARTICIPANTS data, based on a query, 1 for MAINSPORTS data, based on a query, and1 for MAINTRANSPORTATION data, based on a query.
    These three forms work together, pulling up data for the same person, using existing data. BUT, when I add a new person on the MAINPARTICIPANTS form, and go to one of the other forms (MAINSPORTS or MAINTRANSPORTATION), I need it to create a new record in the related table with the same participantID as MAINPARTICIPANTS form. Do I need extra code on the "add new" button on the MAINPARTICIPANTS form to create these related records. If so, can I do this with a macro, rather than VBA?

  7. #7
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142
    I suspect if you use a main form with subforms that access will take care of the rest for you.

    To answer your question, yes you can use a macro as I outlined in my previous reply. In the macro you would have add new record action, then a set field action. You'll find the available list of actions in the right pane in the macro editor.

  8. #8
    msmithtlh is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Mar 2013
    Posts
    151
    To KD2017,
    I would like to use your approach, but am having trouble.
    I created the after-insert macro:

  9. #9
    msmithtlh is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Mar 2013
    Posts
    151
    To KD2017

    My after-insert macro:
    Click image for larger version. 

Name:	macro.PNG 
Views:	21 
Size:	24.4 KB 
ID:	36005

  10. #10
    msmithtlh is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Mar 2013
    Posts
    151
    To KD2017 continued:

    The macro seems to execute before the ID # is assigned. Is there a different property I can use?

  11. #11
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142
    This is more of what I was thinking:

    Just put this macro on the table itself like so:
    Click image for larger version. 

Name:	ex001.png 
Views:	23 
Size:	40.8 KB 
ID:	36006

    This is all that's needed in the macro:

    Click image for larger version. 

Name:	ex002.png 
Views:	21 
Size:	41.6 KB 
ID:	36007

    This is all it takes in Access 2016, I don't use a2010 so I don't know if all these actions are available or not. If table macros aren't available then putting this same macro on the FORM's after insert event (as you have tried above) will still work. If these actions still aren't available then you can still do it with code very easily.

    However, I'm still wondering if you should just be using a form based on the participants table, that has two subforms on it that are based on the other tables. This is how it's usually done.

  12. #12
    msmithtlh is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Mar 2013
    Posts
    151
    To KD2017:

    This is great except that at the point of "after insert" the participantid is still null. Is there a way to combine properties ("after insert" and "dirty")?

  13. #13
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142
    I'm not sure I understand the question.

    Are you trying to do this from the form's after insert event? If so then maybe ParticipantID is null because the form moved on to a new record. The Macro works as presented on my machine when called by the table itself. Have you tried the macro from the table's after insert event and not the form's?

    Can you upload your database?

  14. #14
    msmithtlh is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Mar 2013
    Posts
    151

    copy of my database

    Attached is a zipped copy of my database.
    Attached Files Attached Files

  15. #15
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,892
    Don't see any DataMacros associated with MainParticipants 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.

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. VBA to run two module or sub simultaneously.
    By johnseito in forum Programming
    Replies: 4
    Last Post: 07-18-2018, 01:49 AM
  2. Replies: 6
    Last Post: 02-21-2014, 04:58 PM
  3. Replies: 3
    Last Post: 08-04-2012, 10:06 PM
  4. Run Multiple Queries Simultaneously
    By Shatterday in forum Programming
    Replies: 1
    Last Post: 03-09-2012, 08:54 AM
  5. Replies: 2
    Last Post: 11-14-2011, 03:17 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