我将值传递给Query Parameter
时遇到了一些问题。
我想使用VBA
传递一个值。这就是我的查询:
PARAMETERS [WC Date] DateTime;
SELECT g1.Site_ID, g1.Site_Name, g1.Visits, g1.[Planned Turn], g1.[Avg Planned Turn], g1.[Actual Turn], g1.[Avg Act Turn], g1.[Arrival Variance], g1.[Departure Variance], g1.Week1, g1.Week2, g1.Week3, g1.Week4
FROM (SELECT geo.Site_ID, geo.Site_Name, Count(geo.Site_ID) AS Visits, Sum(DateDiff("n",[Planned_Arrival],[Planned_Departure])) AS [Planned Turn], Round([Planned Turn]/[Visits],2) AS [Avg Planned Turn], Sum(DateDiff("n",[Actual_Arrival],[Actual_Departure])) AS [Actual Turn], Round([Actual Turn]/[Visits],2) AS [Avg Act Turn], Round(Avg(DateDiff("n",[Planned_Arrival],[Actual_Arrival])),2) AS [Arrival Variance], Round(Avg(DateDiff("n",[Planned_Departure],[Actual_Departure])),2) AS [Departure Variance], wk1.Week1, wk2.Week2, wk3.Week3, wk4.Week4
FROM (((T_Geo AS geo LEFT JOIN (SELECT
w1.Site_ID, w1.Site_Name,
Count(w1.Site_ID) AS Visits,
Sum(DateDiff("n",[Actual_Arrival],[Actual_Departure])) AS [Actual Turn],
Round([Actual Turn]/[Visits],2) AS Week1
FROM T_Geo w1
WHERE (((Int([Planned_Arrival]))>=[WC Date]-28 And (Int([Planned_Arrival]))<[WC Date]-21))
GROUP BY w1.Site_ID, w1.Site_Name
) AS wk1 ON geo.Site_ID = wk1.Site_ID) LEFT JOIN (SELECT
w2.Site_ID, w2.Site_Name,
Count(w2.Site_ID) AS Visits,
Sum(DateDiff("n",[Actual_Arrival],[Actual_Departure])) AS [Actual Turn],
Round([Actual Turn]/[Visits],2) AS Week2
FROM T_Geo w2
WHERE (((Int([Planned_Arrival]))>=[WC Date]-21 And (Int([Planned_Arrival]))<[WC Date]-14))
GROUP BY w2.Site_ID, w2.Site_Name
) AS wk2 ON geo.Site_ID = wk2.Site_ID) LEFT JOIN (SELECT
w3.Site_ID, w3.Site_Name,
Count(w3.Site_ID) AS Visits,
Sum(DateDiff("n",[Actual_Arrival],[Actual_Departure])) AS [Actual Turn],
Round([Actual Turn]/[Visits],2) AS Week3
FROM T_Geo w3
WHERE (((Int([Planned_Arrival]))>=[WC Date]-14 And (Int([Planned_Arrival]))<[WC Date]-7))
GROUP BY w3.Site_ID, w3.Site_Name
) AS wk3 ON geo.Site_ID = wk3.Site_ID) LEFT JOIN (SELECT
w4.Site_ID, w4.Site_Name,
Count(w4.Site_ID) AS Visits,
Sum(DateDiff("n",[Actual_Arrival],[Actual_Departure])) AS [Actual Turn],
Round([Actual Turn]/[Visits],2) AS Week4
FROM T_Geo w4
WHERE (((Int([Planned_Arrival]))>=[WC Date]-7 And (Int([Planned_Arrival]))<[WC Date]))
GROUP BY w4.Site_ID, w4.Site_Name
) AS wk4 ON geo.Site_ID = wk4.Site_ID
WHERE (Int([Planned_Arrival])>=[WC Date] And Int([Planned_Arrival])<=[WC Date]+6) AND IsNumeric(geo.Site_ID) = True
GROUP BY geo.Site_ID, geo.Site_Name, wk1.Week1, wk2.Week2, wk3.Week3, wk4.Week4
) AS g1
GROUP BY g1.Site_ID, g1.Site_Name, g1.Visits, g1.[Planned Turn], g1.[Avg Planned Turn], g1.[Actual Turn], g1.[Avg Act Turn], g1.[Arrival Variance], g1.[Departure Variance], g1.Week1, g1.Week2, g1.Week3, g1.Week4
ORDER BY g1.[Avg Act Turn] DESC;
我尝试使用以下代码:
Public Function SendReport()
Dim reportName As String, path As String
Dim wcDate As Date
reportName = "Geofence_Analysis"
wcDate = Date - Weekday(Date, vbSunday) + 1 - 7
path = "C:\users\" & Environ("USERNAME") & "\desktop\" & reportName & "_" & Format(wcDate, "dd-mm-yyyy") & ".pdf"
DoCmd.OpenReport reportName, acViewPreview, , , acWindowNormal, [WC Date] = wcDate
DoCmd.OutputTo acOutputReport, reportName, acFormatPDF, path
End Function
但它在[WC Date]
中抛出错误是我的查询中无法识别的字段。
我知道我可以获得相同的结果设置表单并在那里有一个参数,只是将表单字段引用到我的查询中,但我想从代码中实现...
有什么想法吗?
由于 彼得
答案 0 :(得分:1)
在打开报告之前使用DoCmd.SetParameter
设置参数
Public Function SendReport()
Dim reportName As String, path As String
Dim wcDate As Date
reportName = "Geofence_Analysis"
wcDate = Date - Weekday(Date, vbSunday) + 1 - 7
path = "C:\users\" & Environ("USERNAME") & "\desktop\" & reportName & "_" & Format(wcDate, "dd-mm-yyyy") & ".pdf"
DoCmd.SetParameter "WC Date", wcDate
DoCmd.OpenReport reportName, acViewPreview, , , acWindowNormal
DoCmd.OutputTo acOutputReport, reportName, acFormatPDF, path
End Function