GROUP BY月份报告中的MS Access过滤日期范围

时间:2017-11-28 16:22:47

标签: ms-access access-vba

我很遗憾可能很简单。我按月对结果进行分组,我希望按日期范围对其进行过滤。我有一个如下所示的GROUP BY查询。

PARAMETERS ShipDateStart DateTime, ShipDateEnd DateTime;
SELECT Month([ORDERS].[ShipDate]) & '/01/' & Right(Year([orders].[ShipDate]),2) AS ShipDateName, Sum(IIf([ORDERS].[ShipDate]-IIf(IsNull([ORDERS].[PromisedDate]),[ORDERS].[EstimatedDate],[ORDERS].[PromisedDate])<=0,1,0)) AS OnTimeYes, Sum(IIf([ORDERS].[ShipDate]-IIf(IsNull([ORDERS].[PromisedDate]),[ORDERS].[EstimatedDate],[ORDERS].[PromisedDate])>0,1,0)) AS OnTimeNo, Year([orders].[ShipDate]) AS ShipDateYear
FROM OrderLineStatus INNER JOIN (DEST INNER JOIN ORDERS ON DEST.DestID = ORDERS.DestID) ON OrderLineStatus.OrderLineStatusID = ORDERS.OrderLineStatusID
WHERE (((DEST.Destination)<>"Purchased" And (DEST.Destination)<>"STOCKED") AND ((OrderLineStatus.OrderLineStatus)="Shipped") AND ((ORDERS.PromisedDate) Is Not Null) AND ((ORDERS.ShipDate) Between [ShipDateStart] And [ShipDateEnd]))
GROUP BY Month([ORDERS].[ShipDate]) & '/01/' & Right(Year([orders].[ShipDate]),2), Year([orders].[ShipDate]), Month([ORDERS].[ShipDate])
ORDER BY Year([orders].[ShipDate]), Month([ORDERS].[ShipDate]);

此查询正常。我输入两个参数,它返回我想要的。问题是,当我打开一个带有此查询作为记录源的报表时,它会询问参数,我无法找到一种方法将参数传递给查询而不需要Access。以下不起作用,它仍然要求参数。

sWhere = "[ShipDateStart] = #" & Format(CDate(Me.StartDateBox), "mm/dd/yyyy") & _
       " 00:00# AND [ShipDateEnd] = #" & Format(CDate(Me.EndDateBox), "mm/dd/yyyy") & _
       " 23:59#"
stDocName = "ShippingOnTimePSD"
DoCmd.OpenReport stDocName, acPreview, , sWhere, acWindowNormal

表单上有两个日期控件和一个运行此代码的按钮。怎么办呢?

1 个答案:

答案 0 :(得分:1)

在打开报告之前使用DoCmd.SetParameter设置参数:

DoCmd.SetParameter "ShipDateStart", "#" & Format(CDate(Me.StartDateBox), "mm/dd/yyyy") & "#"
DoCmd.SetParameter "ShipDateEnd", "#" & Format(CDate(Me.EndDateBox), "mm/dd/yyyy") & " 23:59#"
DoCmd.OpenReport stDocName, acPreview, , , acWindowNormal