Search code examples
sqlvbaexcelado

Nested Join in Excel VBA (ADODB) Results In "JOIN expression not supported"


I have a series of three tables which I would like to join together using ADODB in an Excel VBA application. I am using the following query, which is resulting in the "JOIN expression not supported" error:

SELECT    tb1.date, 
          tb1.longID, 
          tb1.fld1,
          tb2.fld2,
          tb3.shortID,
          SUM(tb1.fld3) AS three, 
          SUM(tb1.fld4) AS four, 
          SUM(tb3.fld5) AS five
FROM      ([Table1$] AS tb1 LEFT JOIN [Table2$] AS tb2 ON tb1.longID = tb2.longID)
LEFT JOIN [Table3$]  AS tb3
ON        (tb3.shortID = tb2.shortID AND tb1.date = tb3.date)
GROUP BY  tb1.date, tb1.longID, tb3.shortID, tb2.fld3, tb1.fld2

If I were to omit the shortID column pair, the query works fine. If I omit the date column pair, the query works fine. But as soon as I combine the two, that's when I run into issues.

Any help would be greatly appreciated!

Thanks.


Solution

  • Try to let everything inside the ON part of the query to be inside parenthesis.

    The ON statement in your JOIN operation is incomplete or contains too many tables. You may want to put your ON expression in a WHERE clause.

    SELECT    tb1.date, 
              tb1.longID, 
              tb1.fld1,
              tb2.fld2,
              tb3.shortID,
              SUM(tb1.fld3) AS three, 
              SUM(tb1.fld4) AS four, 
              SUM(tb3.fld5) AS five
    FROM      
    [Table1$] AS tb1 
    LEFT JOIN [Table2$] AS tb2 ON (tb1.longID = tb2.longID)
    LEFT JOIN [Table3$]  AS tb3 ON (tb3.shortID = tb2.shortID)
    WHERE tb1.date = tb3.date
    GROUP BY  tb1.date, tb1.longID, tb3.shortID, tb2.fld3, tb1.fld2