S
Scott Burke
I am updating a program. The program uses VBA to create a query. The
problem is that the orders.[service charge] is being generated with the TOTAL
field = "Expression". If I go into the query design view and change the
TOTAL field to "Group by" the query works perfectly.
Question. Why wont the "Service Charge" be created with TOTAL = "Group by"?
Here is the VBA that creates the query.
Set qd = db.CreateQueryDef("qryCommissionReport")
strSQL = "SELECT IIf(IsNull([SHIPNAME]),[COMPANYNAME],[SHIPNAME]) AS
NAME, "
strSQL = strSQL & "Employees.EmplCode, Orders.PCCOrderNumber,
Orders.[Service Charge], "
strSQL = strSQL & " [FIRSTNAME] & ' ' & [LASTNAME] AS SALESPRSN, "
strSQL = strSQL & "Orders.NewCust, [Order Details].ClearDate2, "
strSQL = strSQL & "Products.ProductName, Customers.ContractNumber, "
strSQL = strSQL & "Sum([Order Details].Quantity) AS SumOfQuantity,
sum(orders.[service charge]) as TOTALSC, "
strSQL = strSQL & "[Order Details].UnitPrice, [Order Details].RAmount, "
strSQL = strSQL & "Employees.Ren, Employees.New, "
strSQL = strSQL & "IIf([REBATE] Is Null,([ORDER
DETAILS].UNITPRICE-[RAMOUNT])*"
strSQL = strSQL & "[QUANTITY],((([ORDER
DETAILS].UNITPRICE-[RAMOUNT])*[QUANTITY])"
strSQL = strSQL & "-[REBATE])) AS PROFIT, "
strSQL = strSQL & "[Order Details].ClearDate, Employees.Location, "
strSQL = strSQL & "Sum(Orders.PkgAmt) AS SumOfPkgAmt, Employees.Cap, "
strSQL = strSQL & "IIf(Not
IsNull([SHIPSTATEORPROVINCE]),[SHIPSTATEORPROVINCE],"
strSQL = strSQL & "[STATEORPROVINCE]) AS STATE "
strSQL = strSQL & "FROM Products INNER JOIN ((Customers INNER JOIN
(Employees "
strSQL = strSQL & "RIGHT JOIN Orders ON Employees.EmployeeID =
Orders.EmployeeID) "
strSQL = strSQL & "ON Customers.CustomerID = Orders.CustomerID) INNER
JOIN "
strSQL = strSQL & "[Order Details] ON Orders.OrderID = [Order
Details].OrderID) ON "
strSQL = strSQL & "Products.ProductID = [Order Details].ProductID "
strSQL = strSQL & "GROUP BY
IIf(IsNull([SHIPNAME]),[COMPANYNAME],[SHIPNAME]), "
strSQL = strSQL & "Employees.EmplCode, Orders.PCCOrderNumber,
Orders.[Service Charge], "
strSQL = strSQL & "[FIRSTNAME] & ' ' & [LASTNAME], "
strSQL = strSQL & "Orders.NewCust, [Order Details].ClearDate2, "
strSQL = strSQL & "Products.ProductName, Customers.ContractNumber, "
strSQL = strSQL & "[Order Details].UnitPrice, [Order Details].RAmount, "
strSQL = strSQL & "Employees.Ren, Employees.New, "
strSQL = strSQL & "IIf([REBATE] Is Null,([ORDER
DETAILS].UNITPRICE-[RAMOUNT])*"
strSQL = strSQL & "[QUANTITY],((([ORDER
DETAILS].UNITPRICE-[RAMOUNT])*[QUANTITY])"
strSQL = strSQL & "-[REBATE])), "
strSQL = strSQL & "[Order Details].ClearDate, Employees.Location,
Employees.Cap, "
strSQL = strSQL & "IIf(Not
IsNull([SHIPSTATEORPROVINCE]),[SHIPSTATEORPROVINCE],"
strSQL = strSQL & "[STATEORPROVINCE]) "
strSQL = strSQL & "HAVING Employees.EmplCode = 'JB' "
strSQL = strSQL & "AND Orders.NewCust = True "
strSQL = strSQL & "AND [Order Details].ClearDate2 Between #2/1/2005# And
#"
strSQL = strSQL & Me.txtEClearDate & "# "
strSQL = strSQL & "AND Customers.ContractNumber <> 'HON-KIP' "
strSQL = strSQL & "AND Employees.Location = 'IN' "
strSQL = strSQL & "ORDER BY Orders.PCCOrderNumber;"
qd.SQL = strSQL
problem is that the orders.[service charge] is being generated with the TOTAL
field = "Expression". If I go into the query design view and change the
TOTAL field to "Group by" the query works perfectly.
Question. Why wont the "Service Charge" be created with TOTAL = "Group by"?
Here is the VBA that creates the query.
Set qd = db.CreateQueryDef("qryCommissionReport")
strSQL = "SELECT IIf(IsNull([SHIPNAME]),[COMPANYNAME],[SHIPNAME]) AS
NAME, "
strSQL = strSQL & "Employees.EmplCode, Orders.PCCOrderNumber,
Orders.[Service Charge], "
strSQL = strSQL & " [FIRSTNAME] & ' ' & [LASTNAME] AS SALESPRSN, "
strSQL = strSQL & "Orders.NewCust, [Order Details].ClearDate2, "
strSQL = strSQL & "Products.ProductName, Customers.ContractNumber, "
strSQL = strSQL & "Sum([Order Details].Quantity) AS SumOfQuantity,
sum(orders.[service charge]) as TOTALSC, "
strSQL = strSQL & "[Order Details].UnitPrice, [Order Details].RAmount, "
strSQL = strSQL & "Employees.Ren, Employees.New, "
strSQL = strSQL & "IIf([REBATE] Is Null,([ORDER
DETAILS].UNITPRICE-[RAMOUNT])*"
strSQL = strSQL & "[QUANTITY],((([ORDER
DETAILS].UNITPRICE-[RAMOUNT])*[QUANTITY])"
strSQL = strSQL & "-[REBATE])) AS PROFIT, "
strSQL = strSQL & "[Order Details].ClearDate, Employees.Location, "
strSQL = strSQL & "Sum(Orders.PkgAmt) AS SumOfPkgAmt, Employees.Cap, "
strSQL = strSQL & "IIf(Not
IsNull([SHIPSTATEORPROVINCE]),[SHIPSTATEORPROVINCE],"
strSQL = strSQL & "[STATEORPROVINCE]) AS STATE "
strSQL = strSQL & "FROM Products INNER JOIN ((Customers INNER JOIN
(Employees "
strSQL = strSQL & "RIGHT JOIN Orders ON Employees.EmployeeID =
Orders.EmployeeID) "
strSQL = strSQL & "ON Customers.CustomerID = Orders.CustomerID) INNER
JOIN "
strSQL = strSQL & "[Order Details] ON Orders.OrderID = [Order
Details].OrderID) ON "
strSQL = strSQL & "Products.ProductID = [Order Details].ProductID "
strSQL = strSQL & "GROUP BY
IIf(IsNull([SHIPNAME]),[COMPANYNAME],[SHIPNAME]), "
strSQL = strSQL & "Employees.EmplCode, Orders.PCCOrderNumber,
Orders.[Service Charge], "
strSQL = strSQL & "[FIRSTNAME] & ' ' & [LASTNAME], "
strSQL = strSQL & "Orders.NewCust, [Order Details].ClearDate2, "
strSQL = strSQL & "Products.ProductName, Customers.ContractNumber, "
strSQL = strSQL & "[Order Details].UnitPrice, [Order Details].RAmount, "
strSQL = strSQL & "Employees.Ren, Employees.New, "
strSQL = strSQL & "IIf([REBATE] Is Null,([ORDER
DETAILS].UNITPRICE-[RAMOUNT])*"
strSQL = strSQL & "[QUANTITY],((([ORDER
DETAILS].UNITPRICE-[RAMOUNT])*[QUANTITY])"
strSQL = strSQL & "-[REBATE])), "
strSQL = strSQL & "[Order Details].ClearDate, Employees.Location,
Employees.Cap, "
strSQL = strSQL & "IIf(Not
IsNull([SHIPSTATEORPROVINCE]),[SHIPSTATEORPROVINCE],"
strSQL = strSQL & "[STATEORPROVINCE]) "
strSQL = strSQL & "HAVING Employees.EmplCode = 'JB' "
strSQL = strSQL & "AND Orders.NewCust = True "
strSQL = strSQL & "AND [Order Details].ClearDate2 Between #2/1/2005# And
#"
strSQL = strSQL & Me.txtEClearDate & "# "
strSQL = strSQL & "AND Customers.ContractNumber <> 'HON-KIP' "
strSQL = strSQL & "AND Employees.Location = 'IN' "
strSQL = strSQL & "ORDER BY Orders.PCCOrderNumber;"
qd.SQL = strSQL