Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Alright, thank you for your answers and suggestions.</p> <p>I am working with an access project connected to MsSQL database. I tried to solve this by using a table trigger, but none of the suggestions has done the trick for me. Therefore I decided to solve this on the client side with VBA code instead.</p> <p>It is probably not the proper way to solve it, but it might be useful to know for anyone reading.</p> <p>The table structure is the same, but I have made a corresponding form for both the product and region table. On the forms AfterInsert event I have the following code:</p> <p>Region table:</p> <pre><code>Private Sub Form_AfterInsert() Dim varRegion As String Dim strSQL As String varRegion = Me![code] strSQL = "INSERT INTO master([region], [product]) SELECT '" &amp; varRegion &amp; "', &amp; _ [code] FROM product;" DoCmd.RunSQL strSQL End Sub </code></pre> <p>Product table:</p> <pre><code>Private Sub Form_AfterInsert() Dim varProduct As String Dim strSQL As String varProduct = Me![code] strSQL = "INSERT INTO master([region], [product]) SELECT [code], &amp; _ '" &amp; varProduct &amp; "' FROM region;" DoCmd.RunSQL strSQL End Sub </code></pre> <p><strong>EDIT:</strong> Having research the matter a little bit more, I found that this is the code you need to be using for the trigger if you are using SQL Server, if you don't want to use the client side setup.</p> <p>Apparently, in SQL Server you need to reference a hidden table called "inserted" when you want to get the values of the inserted row. View this link for more info: <a href="http://msdn.microsoft.com/en-us/library/ms190752.aspx" rel="nofollow noreferrer">Multirow Considerations for DML Triggers</a></p> <p>Great!</p> <p>Product table:</p> <pre><code>-- Trigger statement CREATE TRIGGER "name-of-trigger" ON producttable FOR INSERT AS -- Insert statement INSERT INTO mastertable ([region],[product]) SELECT regiontable.[code], inserted.[code] FROM regiontable, inserted; </code></pre> <p>Region table:</p> <pre><code>-- Trigger statement CREATE TRIGGER "name-of-trigger" ON regiontable FOR INSERT AS -- Insert statement INSERT INTO mastertable ([product],[region]) SELECT producttable.[code], inserted.[code] FROM producttable, inserted; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload