I'm using Access 2007 and am relatively new to this, so bear with me.
The Situation
I've created a simplified example to share.
I have a set of tables in my database that change frequently. Sometimes there are a dozen tables and sometimes only 4 or 5. There is a set of important information that is shared by all of the tables and I want to gather this data into a new table.
Example: Suppose we have the following tables and fields:
Table1 : Name, PhoneNumber, PostalCode,...
Table2 : Name, Phone, PostalCode, Address,...
Table3 : Name, PhoneNo, Address, DateOfBirth,...
Table4 : Name, PhoneNumber, Favorite Food, ...
Etc....
I have written a query to take in the important information(in this case: Name and Phone Number) into a sort of 'master list':
SELECT Name, PhoneNumber
FROM Table1
UNION
SELECT Name, Phone AS PhoneNumber
FROM Table2
UNION
SELECT Name, PhoneNo AS PhoneNumber
FROM Table3
UNION
etc...
;
The Problem
I am trying to find a way to compile this same 'master list' even when some tables aren't in the database. Suppose we have only Table1 and Table3. Is there anyway to add the table to the union query only if it exists? As bad as I am at SQL, I'm even worse at VBA. Somehow I suspect it's possible there, but I thought I'd ask.
Basically, I'm trying to convert this into SQL:
SELECT Name, PhoneNumber
FROM Table1 (IF IT EXISTS)
UNION
SELECT Name, Phone AS PhoneNumber
FROM Table2 (IF IT EXISTS)
UNION
SELECT Name, PhoneNo AS PhoneNumber
FROM Table3 (IF IT EXISTS)
UNION
etc...
;
I get an error message saying that Access cannot find the input table. I figured out that I can use the following code to determine if a table exists:
SELECT Count(*) AS Exists, "Table1" From MsysObjects
WHERE type=1
Any chance the solution involves this?
Thanks in advance!!