I have created an aggregate query to calculate the mode (or most occurrences) of a discount_pct value in a selection of data. I originally had my criteria set in my query in which it compared the OrderHeaderID to a textbox value on an open form. My SQL code for this query is as follows:
Code:
SELECT TOP 1 tblOEOrderDetail.discount_pct
FROM tblOEOrderDetail
GROUP BY tblOEOrderDetail.discount_pct, tblOEOrderDetail.OrderHeaderID
HAVING (((tblOEOrderDetail.OrderHeaderID)=[forms]![frmOrderHeaderEntry]![OrderHeaderIDTxt]))
ORDER BY Count(tblOEOrderDetail.discount_pct) DESC;
I then stored a value in a recordset field by using this query in a dlookup statement in my VBA code as follows:
Code:
rsOEORDHDR("discount_pct") = DLookup("discount_pct", "qryModeOfDiscountPct")
This worked.
I now don't have a form field to base my criteria value on, so I removed the criteria from the query itself. The SQL code for my revised query is as follows:
Code:
SELECT TOP 1 tblOEOrderDetail.discount_pct, tblOEOrderDetail.OrderHeaderID
FROM tblOEOrderDetail
GROUP BY tblOEOrderDetail.discount_pct, tblOEOrderDetail.OrderHeaderID
ORDER BY Count(tblOEOrderDetail.discount_pct) DESC;
I then tried to pass the criteria from a variable (ProcessOrderID, which is numeric) in my VBA subroutine to the query. My revised dlookup statement is as follows:
Code:
rsOEORDHDR("discount_pct") = DLookup("discount_pct", "qryModeOfDiscountPct", "OrderHeaderID = " & ProcessOrderID)
I am getting a NULL value in my recordset field. I can set the criteria in the query to the value that the variable field should be passing to it and I get the correct query value, so I think the problem is my dlookup statement.
Can I pass criteria to an aggregate query this way? If so, what am I doing wrong?
Thanks.