VBA to create a Union queries

prav78

New Member
Joined
Aug 24, 2014
Messages
13
Hi, I need help to write a vba code to create union queries from all the tables in my database.
All the fields are the same for all tables and the table name needs to be a field.

E.g

SELECT Table1.f1, Table1.f2, Table1.f3, "Table1" As f4TName FROM Table1
UNION SELECT Table2.f1, Table.f2, Table2.f3, "Table2" As f4TName FROM Table2
UNION SELECT TableN.f1, TableN.f2, TableN.f3, "TableN" As f4TName FROM TableN;


Thanks.
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Is this what you are looking for:

Code:
strSql = "SELECT Table1.f1, Table1.f2, Table1.f3, ""Table1"" As f4TName FROM Table1 " & vbCrLf & _
"UNION SELECT Table2.f1, Table.f2, Table2.f3, ""Table2"" As f4TName FROM Table2 " & vbCrLf & _
"UNION SELECT TableN.f1, TableN.f2, TableN.f3, ""TableN"" As f4TName FROM TableN;"
 
Upvote 0
Is this what you are looking for:

Code:
strSql = "SELECT Table1.f1, Table1.f2, Table1.f3, ""Table1"" As f4TName FROM Table1 " & vbCrLf & _
"UNION SELECT Table2.f1, Table.f2, Table2.f3, ""Table2"" As f4TName FROM Table2 " & vbCrLf & _
"UNION SELECT TableN.f1, TableN.f2, TableN.f3, ""TableN"" As f4TName FROM TableN;"

Close but the code has to find all the tables and create a union query for all of them.
For example if there are 40 tables, there would be 40 union queries. Is it a For .. Next statement?
 
Upvote 0
Upvote 0
Here is how to loop through all the tables and grab the first 4 fields of each table... Is this what you are looking for??

Code:
Private Sub Foo()

   Dim sql As String
   Dim TableName As String
   Dim tdf As DAO.TableDef
   Dim qdf As DAO.QueryDef
   
   ' Initialize
   sql = ""
   
   ' Iterate the TableDefs collection
   For Each tdf In CurrentDb().TableDefs
      ' Get the table name
      TableName = tdf.Name
      
      ' Be sure it is not a system table
      If (Left(TableName, 4) <> "MSys") And (Left(TableName, 4) <> "USys") Then
         If Len(sql) Then
            sql = sql & " UNION "
         End If
         
         ' Surround table/field names with square brackets
         ' In case there are spaces, etc
         
         ' Grab the first 4 fields from the table
         ' (or you could hard code the names, but might not be reliable)
         sql = sql & " SELECT " & tdf.Fields(0).Name & "]"
         sql = sql & ", [" & tdf.Fields(1).Name & "]"
         sql = sql & ", [" & tdf.Fields(2).Name & "]"
         sql = sql & ", [" & tdf.Fields(3).Name & "]"
         sql = sql & ", '" & TableName & "' As f4TName"
         sql = sql & " FROM [" & TableName & "]"
         
      End If
   Next tdf
   
   ' Save the sql as as query
   Set qdf = New DAO.QueryDef
   qdf.sql = sql
   qdf.Name = "UnionQueryTest"
   CurrentDb().QueryDefs.Append qdf
   CurrentDb().QueryDefs.Refresh

End Sub
 
Upvote 0

Forum statistics

Threads
1,221,821
Messages
6,162,157
Members
451,750
Latest member
pnkundalia

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top