Union Query Help

Def7

Board Regular
Joined
Mar 9, 2009
Messages
61
Is there a way to create a field within the results of a Union Query that shows the name of the table that record came from?
 

Some videos you may like

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type

CT Witter

MrExcel MVP
Joined
Jul 7, 2002
Messages
1,212
You could manually include an additional field in your query:
"Table1" as TableSource

I don't know of a way to make it self-documenting where the query pulls in the name to the table without user intervention.

You could automatically do it if you were generating the query in vba.
 

Def7

Board Regular
Joined
Mar 9, 2009
Messages
61
I have a field on each table already called Project, so the query already comes up with that field, but it is blank since it is blank in the table. I want the query to populate in what table the record is coming from. For example "Project 1 Table" or "Project 2 Table". Is there a way to do that?
 

james_lankford

Well-known Member
Joined
Jan 11, 2009
Messages
1,178
Code:
select 
  "table 1 - " & table1.project as table_project
from 
  table 1

union all

select 
  "table 2 - " & table2.project as table_project
from 
  table 2
 

Def7

Board Regular
Joined
Mar 9, 2009
Messages
61

ADVERTISEMENT

I can't get it to work. I keep getting an error message. This is how I have the code for the entire query:

TABLE [Table 1]
UNION SELECT * FROM [Table 2]
UNION SELECT * FROM [Table 3]
UNION SELECT * FROM [Table 4]

SELECT "Table1 - " & Table1.project as table_project
FROM Table 1
SELECT "Table2 - " & Table2.project as table_project
FROM Table 2
SELECT "Table3 - " & Table3.project as table_project
FROM Table 3
SELECT "Table4 - " & Table4.project as table_project
FROM Table 4;
 

james_lankford

Well-known Member
Joined
Jan 11, 2009
Messages
1,178
Code:
SELECT 
  "Table 1 - " & [Table 1].project as table_project,
  [Table 1].*
FROM 
  [Table 1]

union all

SELECT 
  "Table 2 - " & [Table 2].project as table_project,
  [Table 2].*
FROM 
  [Table 2]

union all

SELECT 
  "Table 3 - " & [Table 3].project as table_project,
  [Table 3].*
FROM 
  [Table 3]

union all

SELECT 
  "Table 4 - " & [Table 4].project as table_project,
  [Table 4].*
FROM 
  [Table 4]

assuming your table names are
[Table 1]
[Table 2]
[Table 3]
[Table 4]
 

Watch MrExcel Video

Forum statistics

Threads
1,122,551
Messages
5,596,797
Members
414,103
Latest member
imamalidadashzada

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
Top