Access 2007 - Left Join to a query returns #Error instead of Null

别等时光非礼了梦想. 提交于 2019-12-02 01:44:49

While the query should return Null based on the join type, as Allen Browne states in his article, Bug: Outer join expressions retrieved wrongly,

"Instead, it behaves as if [the JET query optimizer] is evaluating the expression after it has returned the results from the lower-level query."

Consequently, you must select the calculated field using an expression that will evaluate to Null if the right-side of the join doesn't match the left-side.

Using your pared-down code as an example:

SELECT 
Month.Chain,
DateDiff("m",QueryDates.StartDate,QueryDates.EndDate)+1 AS CasesPerMonthPerStore
FROM
QueryDates,
MonthRange;

SELECT
Chains.Chain,
IIf(IsNull(ErrorQuery.Chain),Null,ErrorQuery.CasesPerMonthPerStore)
FROM
Chains
LEFT JOIN
ErrorQuery
ON Chains.Chain=ErrorQuery.Chain;

It's looking like it could be to do with a known bug in Access, whereby it makes mistakes on outer joins with calculated fields:

http://allenbrowne.com/BugOuterJoinExpression.html

and

http://allenbrowne.com/bug-10.html

So going to see if I can rejig the subqueries to disguise the calculated fields somehow.

I love Access. :)

This is old, but I found a work around that I hope helps somebody. I had the same issue with a right-side query, but I also had a parallel right-side query with a similar data source and derivation that was working correctly in a left join. The difference was there was a UNION query between the working right-side and an underlying query with a simple calculated field. So I put a UNION query between the troublesome right-side query and the final query by creating an empty table with the same fields creating a union with that table and the original right-side query. Worked well. Based on the information from Wilskt and Allen Browne, I think that the UNION is forcing Jet to delay evaluation.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!