在VBA中设置时,Recordsource失败,但在查询设计器

时间:2015-06-24 13:19:43

标签: vba ms-access-2002

编辑:这是通过使用DAO记录集而不是ADO修复的。

我尝试在将输出到Excel工作簿的代码中设置记录源。当VBA命中。打开时,它会出错,"你试图执行一个不包含指定表达式的查询' WP1 + WP2 + WP3 + WP4 + WP5'作为聚合表达式的一部分。 然而,如果我将源粘贴到查询窗口,它可以正常工作。下面我在Source中删除了不相关的字段。

以下是代码中令人讨厌的部分:

With rst
.ActiveConnection = CodeProject.Connection
.CursorType = adOpenKeyset
.LockType = adLockOptimistic
.Source = "SELECT UnderlyingQuery3.WP1, UnderlyingQuery3.WP2, UnderlyingQuery3.WP3, UnderlyingQuery3.WP4, UnderlyingQuery3.WP5 FROM UnderlyingQuery3 ORDER BY [irrelevant fields...]"
.Open Options:=adCmdText
End With

UnderlyingQuery3运行正常。注意,UnderlyingQuery3包含

SELECT (WP1 + WP2 + WP3 + WP4 + WP5) AS WP5Yr FROM (...)

但是,WP6不会继续执行失败的查询。

我尝试构建另一个查询,而不是将SQL写入源代码。该查询在查询窗口中工作正常。但是,将该查询设置为源(使用Options:= adCmdStoredProc)仍然会失败并出现相同的错误。

我还演了一部Compact&修复和反编译/重新编译无效。我没有证据表明可能存在腐败现象。

UnderlyingQuery3的结构:

SELECT 
UnderlyingQuery2.WP1, 
UnderlyingQuery2.WP2, 
UnderlyingQuery2.WP3, 
UnderlyingQuery2.WP4, 
UnderlyingQuery2.WP5, 
UnderlyingQuery2.WP5Yr, 
FROM UnderlyingQuery2;

UnderlyingQuery2的结构:

SELECT 
(CENSORED FIELDS), 
Sum(DATA.WP1) AS WP1, 
Sum(DATA.WP2) AS WP2, 
Sum(DATA.WP3) AS WP3, 
Sum(DATA.WP4) AS WP4, 
Sum(DATA.WP5) AS WP5,
WP1+WP2+WP3+WP4+WP5 AS WP5Yr 
FROM UnderlyingQuery1 AS DATA 
GROUP BY (CENSORED FIELDS) 
UNION ALL 
SELECT 
(CONSTANTS REPRESENTING AGGREGATES OF CENSORED FIELDS),
Sum(DATA.WP1) AS WP1, 
Sum(DATA.WP2) AS WP2, 
Sum(DATA.WP3) AS WP3, 
Sum(DATA.WP4) AS WP4, 
Sum(DATA.WP5) AS WP5 
FROM UnderlyingQuery1 AS DATA

我正在努力修复格式,但在这里!我真的认为UnderlyingQuery1在这里无关紧要。它只是将原始数据表内部连接到参数表,以选择某些年份和数据的其他标准

0 个答案:

没有答案