0

I have the following SQl query in VBA and it gives me a syntax error when I run the form. I tried writing the query again, but I ended up with the same error. Any help would be appreciated.Thank you:

 Set RS = CurrentDb.OpenRecordset("SELECT & _
          Purchases.SupplierLocation & _
        , Purchases.MinWeight & _
        , Purchases.MaxWeight & _
        , Purchases.PortofDischarge & _
        , Purchases.PlaceofDelivery & _
        , Purchases.PriceTermsLocation " & _

         "FROM ( & _
          Vendors & _
          RIGHT JOIN Purchases & _
             ON Vendors.VendorID=Purchases.VendorID & _
          ) & _
          INNER JOIN & _
              (SELECT Max(PODate) as MaxPODate & _
               FROM Purchases & _
               WHERE Purchases.VendorID = '" & Nz(Me.VendorID, "") & "' & _
               AND Purchases.Grade = '" & Nz(Me.Grade, "") & "' & _
               AND Purchases.PurchaseID <> " & Nz(Me.PurchaseID, 0) & " & _
              ) RecentPurchase & _
             ON Purchases.PODate = RecentPurchase.MaxPODate " & _
         "WHERE Purchases.VendorID = '" & Nz(Me.VendorID, "") & "' & _
          AND Purchases.Grade = '" & Nz(Me.Grade, "") & "' & _
          AND Purchases.PurchaseID <> '" & Nz(Me.PurchaseID, 0)"' & _
         "ORDER BY Min(IIf(Vendors.VendorID=Purchases.VendorID,1,2))")
1
  • I hope this isn't too rude, but you would probably be able to easily troubleshoot your code if it was well-formatted... it certainly couldn't hurt at least. Commented May 9, 2017 at 18:26

2 Answers 2

1

As @JacobH stated it could be clearer but it could be that just before the order by :

"' & _ "ORDER BY 

should be

"'" & _ "ORDER BY
Sign up to request clarification or add additional context in comments.

2 Comments

Good spotting, you have better eyes than I do!
Tried it. It is still giving me the same syntax error.
0

you missed a lot of double quotes:

Set RS = CurrentDb.OpenRecordset("SELECT " & _
              "Purchases.SupplierLocation " & _
            ", Purchases.MinWeight " & _
            ", Purchases.MaxWeight " & _
            ", Purchases.PortofDischarge " & _
            ", Purchases.PlaceofDelivery " & _
            ", Purchases.PriceTermsLocation " & _
             "FROM ( " & _
              "Vendors " & _
              "RIGHT JOIN Purchases " & _
               "  ON Vendors.VendorID=Purchases.VendorID " & _
              ") " & _
              "INNER JOIN " & _
                  "(SELECT Max(PODate) as MaxPODate " & _
                  " FROM Purchases " & _
                  " WHERE Purchases.VendorID = '" & Nz(Me.VendorID, "") & "' " & _
                  " AND Purchases.Grade = '" & Nz(Me.Grade, "") & "' " & _
                  " AND Purchases.PurchaseID <> " & Nz(Me.PurchaseID, 0) & _
                 " ) RecentPurchase " & _
                " ON Purchases.PODate = RecentPurchase.MaxPODate " & _
             "WHERE Purchases.VendorID = '" & Nz(Me.VendorID, "") & "' " & _
             " AND Purchases.Grade = '" & Nz(Me.Grade, "") & "' " & _
             " AND Purchases.PurchaseID <> '" & Nz(Me.PurchaseID, 0) & "' " & _
             "ORDER BY Min(IIf(Vendors.VendorID=Purchases.VendorID,1,2))")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.