SQL Query-Ms Access中的语法错误

时间:2017-05-09 18:22:55

标签: sql vba ms-access

我在VBA中有以下SQl查询,它在运行表单时给出了语法错误。我尝试再次编写查询,但我最终得到了同样的错误。任何帮助将不胜感激。谢谢你:

 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))")

2 个答案:

答案 0 :(得分:1)

正如@JacobH所说,它可能更清晰但可能就在订单之前:

"' & _ "ORDER BY 

应该是

"'" & _ "ORDER BY

答案 1 :(得分:0)

你错过了很多双引号:

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))")