Results 1 to 10 of 10
  1. #1
    Parminder is offline Novice
    Windows 8 Access 2013
    Join Date
    Jul 2015
    Posts
    8

    Insertion operation in multiple tables holding many-to-many relationship

    Hello all



    I am developing something in which I have to perform insertion operation on tables having many-to-many relation. How can I perform this?

    As an example,

    I have 3 tables like:
    1st
    Click image for larger version. 

Name:	Capture.JPG 
Views:	7 
Size:	17.3 KB 
ID:	21503

    2nd: Bridge table
    Click image for larger version. 

Name:	Bridge.JPG 
Views:	7 
Size:	10.4 KB 
ID:	21504

    3rd: ID table
    Click image for larger version. 

Name:	ID.JPG 
Views:	7 
Size:	13.5 KB 
ID:	21505

    I want to perform insertion operation on bridge table on selection of items from listbox like, suppose the ID is 2 and they can select any number of values from listbox and that selected items should be inserted into bridge table.
    How can I perform this operation?
    Will wait for some positive and useful reply.

    Regards
    Parminder

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    User selects ID (from a combobox?) and then selects multiple items from a multi-select listbox? Then you want to write records to Bridge table? This will require code that loops through the items of the listbox and runs SQL INSERT action to create records.

    Here is example of looping through listbox: http://bytes.com/topic/access/answer...-thru-list-box

    Instead of opening a filtered report, you would run SQL statement.

    CurrentDb.Execute "INSERT INTO Bridge(ID, sk_weather) VALUES(" & Me.ID & ", " & lst.ItemData(vItem) & ")"
    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
    Parminder is offline Novice
    Windows 8 Access 2013
    Join Date
    Jul 2015
    Posts
    8
    The problem I have is ID is auto generated and I want to take that value from 3rd ID table. Is there any way to do that?

  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,930
    ID is autonumber in ID table and is PK?

    This is saved as FK in Bridge table?

    What I suggested should work.

    The real trick is figuring out what event to put code into. If user is not selecting ID from a combobox, then where will it come from - how will it be selected/specified for use in writing records to Bridge?
    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
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    Parminder,

    Can you forget the tables and Access for the moment, and tell readers in business terms, plain English
    (no jargon) WHAT it is that you are trying to accomplish? There are some details, as June has mentioned, that are not clear, but it would be better if we understood your requirement.

  6. #6
    Parminder is offline Novice
    Windows 8 Access 2013
    Join Date
    Jul 2015
    Posts
    8
    Yes sure,

    In actual I want user to select multiple items and store those selected items somewhere, there is many to many relationship between two entities, as an example one book can have more than one author and one author can write more than one book and this can obtain duplicate entries. So in order to remove duplicacy, I want to create one table which will contain just two columns as an example bookID and aurthorID and the combination of these two columns will be unique.
    I want to insert that information into table through user forms not manually.
    The problems I am facing is:
    1. I created one table, i.e staging table from where I am getting ID, which is a field in staging table and it is autonumber, I am confused that how can I get that information from table.
    2. I am using listbox for multiple selections and they are associated with weather table and connected to sk_weather column.
    So I want to get ID from staging table and it will be joined with sk_weather and the combination of these two will be unique and stored in another new table which is Bridge table.

    I hope you guys understand the scenario, But still if you guys have any question feel free to ask.

    Regards
    Parminder

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Did you see post 4?
    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
    Parminder is offline Novice
    Windows 8 Access 2013
    Join Date
    Jul 2015
    Posts
    8
    Quote Originally Posted by June7 View Post
    Did you see post 4?
    Yes, I did see the post. I am trying to figure out how can I retrieve value of ID from ID table. It is autogenerated and PK in ID table and referred as a foreign key in Bridge table.

  9. #9
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    StagingTable(source of ID)--->BridgeTable(junction table)<----WeatherTable(rowsource of listbox)

    I think post #2 identifies the solution.
    You just have to refer to the fields by name in the SQL/vba. You do not have to be concerned with how the autonumber assignment occurs/occurred.

    Please show us what you have done if there are still issues.

  10. #10
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Well, if doing data entry on form bound to ID table, code can be behind that form. The real trick is figuring out what event to put code into. The ID table record must first be committed to table before saving the related child records. Could be a button Click.

    DoCmd.RunCommand acCmdSaveRecord
    If IsNull(DLookup("ID", "Bridge", "ID=" & Me.ID)) Then
    'looping code to read values from list box and save records
    End If
    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-19-2013, 01:14 PM
  2. Replies: 4
    Last Post: 12-12-2012, 10:21 AM
  3. Replies: 5
    Last Post: 12-05-2012, 10:42 AM
  4. Creating relationship for multiple tables
    By dave john in forum Programming
    Replies: 1
    Last Post: 09-02-2012, 08:18 AM
  5. Replies: 5
    Last Post: 10-04-2011, 12:53 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