- construct the query in MS Query using inner joins only;
- manually change the inner join details in the SQL generated by MS Query to outer joins
SELECT Data.ID, Data.DName, Data2.D2Name, Data3.D3Name
FROM Data.txt Data, Data2.txt Data2, Data3.txt Data3
WHERE Data.ID = Data2.ID AND Data.ID = Data3.ID
After editing the SQL became:
SELECT Data.ID, Data.DName, Data2.D2Name, Data3.D3Name
FROM (Data.txt Data
left outer join Data2.txt Data2 on Data.ID = Data2.ID)
left outer join Data3.txt Data3 on Data.ID = Data3.ID
The output of this query looks as follows:
ID | DName | D2Name | D3Name |
1 | Unrestricted | Unrestricted | |
2 | Restricted | Restricted | |
3 | Designated | Designated |
The outer join operation is evidenced by the null values returned under D2Name and D3Name.
For Excel 2003 I make the changes to the SQL by opening an Immediate window in the Visual Basic Editor and typing something like [?activecell.querytable.commandtext] followed by [Enter]. This reports the first SQL above on rows two to four of the window. Then I edit the first line to read [activecell.querytable.commandtext = "SELECT Data.ID, Data.DName, Data2.D2Name, Data3.D3Name FROM (Data.txt Data left outer join Data2.txt Data2 on Data.ID = Data2.ID) left outer join Data3.txt Data3 on Data.ID = Data3.ID"] followed by [Enter]. This changes the SQL without the need to use MS Query. A simple refresh of the data will show the multitable query with the outer joins in place.
I hope you will find this helpful. It has always worked for me, but I worry that there must be some good reason that MS Query complains about such a query.
Your comments would be very welcome
I have been looking for the past few months on how to get this to work, thank you for ending my turmoil!
ReplyDeleteYou are most welcome. Having beaten myself up over this for ages too, it is nice to know I've helped end someone else's turmoil!
DeleteThanks for this great post. Following the example solved my problem in about 5 minutes. MS Query will not show the join, but it works in Excel!
ReplyDeleteYou are great!!!! Thank you so much!!
ReplyDeleteWhy does "Microsoft Access" allow outer joins when multiple tables are in the query and "Microsoft Query" doesn't?
ReplyDeleteI'm sorry, I have no idea why that should be so.
DeleteIan - this is a great help! Thankyou.
ReplyDeleteAndrew Hills
If You write the query in MS-Query as a derived table it will work perfectly with multiple outer joins.
ReplyDeleteGreat! I was looking for this solution for couple months. Thanks!
ReplyDelete