我有一个关于在MS Access 2010中创建运行总数的问题,类似于这里的问题:
Access 2010-查询显示多条记录的运行总数,删除旧记录并在每条上添加新记录
然而,当我从该线程输入等效代码时,我会收到一个错误,说找不到数据库(Access似乎认为我指定的表是数据库名称)
以下是来自原始线程的代码:-
SELECT hbep1.EmployeeID, hbep1.PayPeriodID,
(
SELECT Sum(hbep2.HoursUsed)
FROM Hours_by_Empl_PP hbep2
WHERE hbep2.EmployeeID=hbep1.EmployeeID
AND (hbep2.PayPeriodID Between hbep1.[PayPeriodID]-3
And hbep1.[PayPeriodID])
) AS Sum_of_Hours_last_4_PPs
FROM Hours_by_Empl_PP hbep1;
这是我在查询中输入的代码:-
SELECT
V4_Try.ID_NIS_INV_HDR,
V4_Try.ID_ITM,
V4_Try.RunTot3,
V4_Try.BomVsActQty,
DMin("RunTot3","V4_Try","[ID_Itm]=" & [ID_ITM]) AS IDItmMin,
DMax("RunTot3","V4_Try","[ID_Itm]=" & [ID_ITM]) AS IDItmMax,
(
SELECT Sum([V4_Try].[BomVsActQty])
FROM [V4_Try].[BomVsActQty]
WHERE [V4_Try].[ID_ITM]=[V4_Try].[ID_ITM]
AND (IDItmMax < IDItmMin)
) AS RunTot6
FROM V4_Try
ORDER BY V4_Try.ID_ITM, V4_Try.RunTot3;
我注意到的一件事是,主查询使用DMax()
和DMin()
来创建一些别名列
...
DMin("RunTot3","V4_Try","[ID_Itm]=" & [ID_ITM]) AS IDItmMin,
DMax("RunTot3","V4_Try","[ID_Itm]=" & [ID_ITM]) AS IDItmMax,
...
然后子查询尝试在其WHERE子句中使用这些别名
(
SELECT ...
WHERE...
AND (IDItmMax < IDItmMin)
) AS RunTot6
我确信子查询不知道"父"查询中的列别名,所以它们可能是无法识别的项。
通过运行以下查询开始:
SELECT * FROM V4_Try;
然后针对复杂性进行开发。首先生成嵌套查询。当你知道它在运行时,试着添加别名,然后添加DMax()
函数,依此类推。隔离出现错误的点。
这是修复查询的过程。
哦,请指定Access引发的确切错误。此外,如果这是从VBA运行的,请告诉我们,因为这会影响您的故障排除。