Skip to content
Advertisement

VBA SQL: Syntax Error in From Clause, Double Inner Join with Password

I’m reasonably new to SQL and I’m trying to create a string that collects the following:

  • Code from [Catalogue Info] as c
  • Description from [Product Information] as p
  • Weight from p
  • PPB from p
  • CP-UK from p
  • CP-EU from p
  • 1 from an external password protected database C:mypathdb.accdb as pl

The code below keeps giving me a Syntax error in From clause. I’m assuming this is something to do with my brackets around the INNER JOINS but I’m not sure.

This code worked perfectly fine before adding in the second INNER JOIN clause (external DB), My WHERE & ORDER BY clauses work fine.

                sqlProd = "SELECT c.Code," _
            & " p.Description, p.weight, p.[Pack Size], p.PPB, p.[CP-UK], p.[CP-EU]," _
            & " pl.1" _
            & " FROM ([Catalogue Info] c" _
            & " INNER JOIN [Product Information] p" _
                & " on c.code = p.code)" _
            & " INNER JOIN [;database=C:mypathdb.accdb;PWD=password123].table_name pl" _
                & " on c.code = pl.code" _
            & " WHERE c.Sub_Cat_1 = '" & rstSub1!Sub_Cat_1 & "'" _
            & " AND c.Sub_Cat_2 = '" & rstSub2!Sub_Cat_2 & "'" _
            & " ORDER BY c.Page ASC, c.Page_Position ASC;"

Any thoughts?

Access 2016, Excel 2016, Windows 10

Thanks! Dom

Advertisement

Answer

I think I have found a solution to this – it wasn’t the bracketing, it was the password I was using.

The password I was using (not in original question) used special characters (#>`{(= which seemed to be giving the syntax error. I changed the password in my Access Database to letters/numbers only and the original code works fine.

Hope this helps anyone else out. Final code:

sqlProd = "SELECT c.Code," _
        & " p.Description, p.weight, p.[Pack Size], p.PPB, p.[CP-UK], p.[CP-EU]," _
        & " pl.[1]" _
        & " FROM [Catalogue Info] as c" _
        & " INNER JOIN [Product Information] as p" _
        & " on c.code = p.code" _
        & " INNER JOIN [;database=C:mypathdb.accdb;PWD=password123].table_name as pl" _
        & " on c.code = pl.code" _
        & " WHERE c.Sub_Cat_1 = '" & rstSub1!Sub_Cat_1 & "'" _
        & " AND c.Sub_Cat_2 = '" & rstSub2!Sub_Cat_2 & "'" _
        & " ORDER BY c.Page ASC, c.Page_Position ASC;"

Thanks for your feedback.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement