I am writing a sub that allows the user to select a Customer from a listbox. The selection is recorded as CustomerID (integer variable) and used to query an Access Database file. The sub should then output to an excel worksheet sales information about the specified customer, notably:
- Order Date
- OrderID
- Total Order Cost (defined as qty sold * Price Sold)
The access file has 3 tables that I need: Customers, Orders, LineItems
My code below should join the customer ID to order ID fields, which is then joined against order ids, and order ids is joined on line items.
' Define SQL statement to get order info for selected product.
SQL = "SELECT O.OrderDate, COUNT(O.OrderID), SUM(L.QuantityOrdered * L.QuotedPrice) AS [TotalCost] " _
& "FROM (((Customers C INNER JOIN Orders O ON C.CustomerID = O.CustomerID) " _
& "INNER JOIN ON O.OrderID = O.CustomerID) INNER JOIN LineItems L " _
& "ON O.OrderID = L.OrderID)" _
& "WHERE O.CustomerID =" & CustomerID & " " _
& "GROUP BY O.OrderDate, O.OrderID" _
& "ORDER BY O.OrderDate"
I keep getting a "Syntax Error in FROM clause". Are my JOIN statements are correct? I have played around with the (), "", etc without success. I have checked and Table Names are correct (Orders, Customers, LineItems) field names are also spelled correctly.
ON
clause andWHERE
, or between group by and order by. – AdamMc331debug.print sql
directly under this and check the VBE's Immediate window (Ctrl+G) to view what you have produced. Paste it into a blank Access query and modify until it works then transfer the changes back to the string construction and retest. – user4039065