MS Access - 从联合查询中提取最小/最大年份到报告

时间:2018-04-06 13:25:28

标签: sql database ms-access ms-access-2010

我有一个联合查询,它从具有相似字段的查询和表中提取对象及其日期,以便我可以在报表上一起显示它们。联合查询(qry_ObjectUnion)有一个WHERE函数,提示用户输入开始和结束日期。

SELECT [Object], [DateObject]
FROM qry_ObjectQuery
WHERE (((Year([DateObject])) Between [Which year would you like this report to start?] And [Which year would you like this report to end?]))

UNION SELECT [Object], [DateObject]
FROM tbl_ObjectTable
WHERE (((Year([DateObject])) Between [Which year would you like this report to start?] And [Which year would you like this report to end?]));

在报告中,我想动态显示用户为报告选择的开始和结束年份。为了实现这一点,我在联合查询中编写了第二个查询(qry_ObjectUnionYears)。

SELECT Min(Year([DateObject])) AS MinYear, Max(Year([DateObject])) AS MaxYear
FROM qry_ObjectUnion;

第二个查询在直接访问(双击)查询时按预期返回开始和结束年份,但打开报告会产生不同的结果。在报告中,我有一个带有MinYear控制源的文本框:

=[qry_ObjectUnionYears]![MinYear]

访问报告时,前两个消息框询问开始和结束年份,但第三个消息框询问qry_ObjectUnionYears的参数值,并继续返回#Error,无论我输入什么。

任何人都可以帮助我吗?提前谢谢!

1 个答案:

答案 0 :(得分:1)

我在网上浏览后想出来了。文本框可以自行引用输入到报告中的参数,如下所示:

=Report![ObjectReport]![Which year would you like this report to start?]

没有必要像我最初尝试的那样编写第二个查询。