Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Create an Access Module and use the following code. Replace the values in the test sub with your table and destination names</p> <pre><code>Option Compare Database Option Explicit Function SplatTablesSql(pT1 As String, pT2 As String, pDest As String) Dim lDb As Database Dim lTd1 As TableDef, lTd2 As TableDef Dim lField As Field, lF2 As Field Dim lS1 As String, lS2 As String, lSep As String SplatTablesSql = "Select " lS1 = "Select " lS2 = "Select " Set lDb = CurrentDb Set lTd1 = lDb.TableDefs(pT1) Set lTd2 = lDb.TableDefs(pT2) For Each lField In lTd1.Fields SplatTablesSql = SplatTablesSql &amp; lSep &amp; "x.[" &amp; lField.Name &amp; "]" lS1 = lS1 &amp; lSep &amp; "a.[" &amp; lField.Name &amp; "]" Set lF2 = Nothing On Error Resume Next Set lF2 = lTd2.Fields(lField.Name) On Error GoTo 0 If lF2 Is Nothing Then lS2 = lS2 &amp; lSep &amp; "Null" Else lS2 = lS2 &amp; lSep &amp; "b.[" &amp; lField.Name &amp; "]" End If lSep = ", " Next For Each lField In lTd2.Fields Set lF2 = Nothing On Error Resume Next Set lF2 = lTd1.Fields(lField.Name) On Error GoTo 0 If lF2 Is Nothing Then SplatTablesSql = SplatTablesSql &amp; lSep &amp; "x.[" &amp; lField.Name &amp; "]" lS1 = lS1 &amp; lSep &amp; "Null as [" &amp; lField.Name &amp; "]" lS2 = lS2 &amp; lSep &amp; "b.[" &amp; lField.Name &amp; "]" End If lSep = ", " Next SplatTablesSql = SplatTablesSql &amp; " Into [" &amp; pDest &amp; "] From ( " &amp; lS1 &amp; " From [" &amp; pT1 &amp; "] a Union All " &amp; lS2 &amp; " From [" &amp; pT2 &amp; "] b ) x" End Function Sub Test() CurrentDb.Execute SplatTablesSql("a", "b", "c") End Sub </code></pre>
    singulars
    1. This table or related slice is empty.
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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