title: Create and run append queries for multiple linked tables
body: <p>I am trying to write a VBA code in Microsoft Access that will create and run 60 queries to select new records from 60 linked tables and insert them into 60 tables of the same format. Some background may help here:</p>
  <p>I have a large database (lets call the original database &quot;A&quot;) that will eventually have over 60 tables, 60 forms &amp; 60 reports. When one of our workers goes out into the field and doesn't have an internet connection, they are going to create new records on a copy of the database stored on their desktop (lets call the duplicate database &quot;B&quot;). Once they have an internet connection, I want them to be able to press a button on either database (I have been trying to code the macro on database A because I thought that would be easiest) and have the new records they created on database B inserted into database A.</p>
  <p>I have found <a href="https://stackoverflow.com/questions/2670716/how-to-merge-data-from-two-separate-access-2007-databases">code</a> online that seems to be just what I need but when I try to run the macro it gives Error 3022, which says:</p>
  <blockquote>
  <p>The changes you requested to the table were not successful because they would create duplicate values in the index, primary key, or relationship</p>
  </blockquote>
  <p>I have tried running this macro with both databases on my desktop with only 1 linked table with a primary key that is an autonumber, I tried running it with a random autonumber, I tried not having any primary key or index or autonumber and even no records at all. I even tried running it without any linked tables. All ways of trying give me the same Error 3022. I really don't want to create 60 queries one by one so any help would be greatly appreciated. Thank you wizards in advance :)</p>
  <p>Here is the code I have tried:</p>
  <pre><code>Public Sub ImportTableData(ByVal pstrTable As String, ByVal pstrDb As String)
  
      Dim strSql As String
      
      strSql = &quot;INSERT INTO &quot; &amp; pstrTable &amp; vbNewLine &amp; _
          &quot;SELECT *&quot; &amp; vbNewLine &amp; _
          &quot;FROM &quot; &amp; pstrTable &amp; &quot; IN '&quot; &amp; pstrDb &amp; &quot;';&quot;
  
      CurrentDb.Execute strSql, dbFailOnError
      
  End Sub
  
  Public Sub ImportAllTables()
      Const cstrDb As String = &quot;C:\MyPath\DatabaseB.accdb&quot;
      Dim tdf As TableDef
      Dim strMsg As String
  
  On Error GoTo ErrorHandler
  
      For Each tdf In CurrentDb.TableDefs
          'ignore system and temporary tables '
          If Not (tdf.Name Like &quot;MSys*&quot; Or tdf.Name Like &quot;~*&quot;) Then
              Call ImportTableData(tdf.Name, cstrDb)
          End If
      Next tdf
  
  ExitHere:
      On Error GoTo 0
      Set tdf = Nothing
      Exit Sub
  
  ErrorHandler:
      Select Case Err.Number
      Case 3078
          strMsg = &quot;Input table &quot; &amp; tdf.Name &amp; &quot; not found.&quot;
          MsgBox strMsg
          Resume Next
      Case Else
          strMsg = &quot;Error &quot; &amp; Err.Number &amp; &quot; (&quot; &amp; Err.Description &amp; &quot;) in procedure ImportAllTables&quot;
          MsgBox strMsg
          GoTo ExitHere
      End Select
      
  End Sub
  </code></pre>
  <p>When I remove the piece of code: 'dbFailOnError', I get different errors. First thing that pops up when I run the macro is: ''Input table 'LocalTableName' not found'', which is the table I am trying to add the records to. Once I click 'Ok' on that pop up box, that is when Error 3134 pops up which says 'Syntax error in INSERT INTO statement'. I am assuming Error 3134 only pops up because it cannot find the local input table (the fist pop up box).</p>
  <p>Also, I tried changing the line of code that says: 'Const cstrDb As String = ''C:\MyPath\DatabaseB.accdb''' to instead point to database A (which is the one I am coding the macro on) like this: 'Const cstrDb As String = ''C:\MyPath\DatabaseA.accdb'''. This doesn't give me the first pop up that says ''Input table 'LocalTableName' not found'' but it still gives Error 3134.
  I have no idea what I am doing wrong and have spent over 20 hours on this problem trying dozens of different things. Any help would be greatly appreciated :)</p>