使用表单过滤交叉表查询 MS Access 2010

Posted

技术标签:

【中文标题】使用表单过滤交叉表查询 MS Access 2010【英文标题】:Using a Form to Filter a Crosstab Query MS Access 2010 【发布时间】:2014-06-10 20:58:21 【问题描述】:

我正在使用 Access 2010。我有一个交叉表查询,我试图根据表单上文本框的值进行过滤。完整的SQL如下:

PARAMETERS Forms!SelectForm!Text27 DateTime;

TRANSFORM nz(Sum([Raw Contract Data].[Action Obligation]), 0) AS [SumOfAction Obligation]

SELECT [Raw Contract Data].[NAICS Code], [Raw Contract Data].[NAICS Description], Sum([Raw
Contract Data].[Action Obligation]) AS [Total Of Action Obligation], NZ(round(avg([SumOfAction 
Obligation]),0),) AS [Average Obligation], 
nz(round(stdev([SumOfAction Obligation]),0),0) AS [StDev of CTRs], 
nz(round(var([SumOfAction Obligation]),0),0) AS [Variance of Obligations]

FROM [Raw Contract Data]

WHERE [Raw Contract Data].[Date Signed]  <= Forms!SelectForm!Text27

GROUP BY [Raw Contract Data].[NAICS Code], [Raw Contract Data].[NAICS Description]

PIVOT format([Raw Contract Data].[Date Signed], "yyyy-mm");

问题是查询会运行,但会弹出一个框,询问文本 27 的值。它无法从表单中的值识别它。当我手动输入日期(没有前面的 PARAMETERS 声明)时,WHERE 子句将起作用,但是当我希望它引用文本字段的值时,我什么也得不到。

【问题讨论】:

【参考方案1】:

试试这个方法,如果对你有帮助,请告诉我

PARAMETERS EnterDate DateTime;
TRANSFORM nz(Sum([Raw Contract Data].[Action Obligation]), 0) AS [SumOfAction Obligation]
SELECT [Raw Contract Data].[NAICS Code], [Raw Contract Data].[NAICS Description],
Sum([RawContract Data].[Action Obligation]) AS [Total Of Action Obligation],
NZ(round(avg([SumOfAction Obligation]),0),) AS [Average Obligation],
nz(round(stdev([SumOfAction Obligation]),0),0) AS [StDev of CTRs],
nz(round(var([SumOfAction Obligation]),0),0) AS [Variance of Obligations]
FROM [Raw Contract Data]
WHERE [Raw Contract Data].[Date Signed]  <= EnterDate
GROUP BY [Raw Contract Data].[NAICS Code], [Raw Contract Data].[NAICS Description]
PIVOT format([Raw Contract Data].[Date Signed], "yyyy-mm");

保存这个查询说名字Query1

On Filter action say on clicking filter button定义如下代码

Dim qdf As DAO.QueryDef
Dim rst as DAO.Recordset
Set qdf = CurrentDb.QueryDefs("Query1")
qdf!EnterDate = Forms!SelectForm!Text27
Set rst = qdf.OpenRecordset
Set rst = Nothing
Set qdf = Nothing

【讨论】:

可以用宏代替vba吗?【参考方案2】:

请记住,您引用 Text27

的表单必须处于打开状态

否则当你执行查询时,由于表单没有打开,查询没有得到参数值,它会询问你Text27的值。

【讨论】:

是的。我有它处于打开状态。

以上是关于使用表单过滤交叉表查询 MS Access 2010的主要内容,如果未能解决你的问题,请参考以下文章

数据表视图中的 MS Access 表单交叉表查询

基于包含子查询的查询的 ms-access 交叉表查询

从 MS Access 中将交叉表查询结果导出到 Excel

MS Access 参数查询导致错误

使用宏过滤带有组合框的表单 - MS Access

TSQL 等效于 MS Access 交叉表查询