"无效使用Null"在Access Query中

时间:2014-10-17 20:06:57

标签: sql ms-access ms-access-2007 ms-access-2000

我有两个几乎相同的数据库(第二个是第一个的副本,然后使用不同的公司/产品信息进行修改)。有一个老化的报告,从来没有正常工作,我不得不修复。我让它在一个数据库(A)上工作,但第二个(B)启动了“无效”错误。

报告查询是从另一个查询生成的,该查询基本上可以获取订单信息和客户信息。从那里,有问题的查询将订单拆分成块,具体取决于它们发货的时间,并返回余额。弹出的错误消息包含以下代码段:

Round(Sum(IIf((Date()-[ShipDate] Is Null),[LineTotal]+(CLng([LineTotal]*[SalesTaxRate]*100)/100)+[FreightCharge]-nz([Total Payments]),0)),2) AS Current_Unshipped

如果Is Null更改为其他任何内容,则查询将正常执行。如果我尝试引入任何“如果变量为空”代码,它会抛出错误。我试过以下,但无济于事。

Round(Sum(IIf((nz[ShipDate]),[LineTotal]+(CLng([LineTotal]*[SalesTaxRate]*100)/100)+[FreightCharge]-nz([Total Payments]),0)),2) AS Current_Unshipped

如果shipdate为null,我有另一列抛出“NULL”,这令人惊讶地起作用。第一个代码段在另一个数据库中完美运行,所以我很困惑。我需要能够为未发货的订单生成一列,因此缺少[ShipDate]。我不知道为什么代码不起作用。

整个

(CLng([LineTotal]*[SalesTaxRate]*100)/100)+[FreightCharge]-nz([Total Payments]),0))

代码的一部分是如何生成余额,它是有效的。它也在数据库中的大约一百万个位置实现 - 设计者认为每次运行查询时都会自动生成总余额,而不是将其存储在某个地方。

整个查询代码也会在下面发布。如果有人能指出我正确的方向,那将是非常有帮助的。

SELECT DISTINCTROW 
    Round(Sum(IIf((Date()-[ShipDate]<31),[LineTotal]+(CLng([LineTotal]*[SalesTaxRate]*100)/100)+[FreightCharge]-nz([Total Payments]),0)),2) AS [Current],
    Round(Sum(IIf((Date()-[ShipDate] Is Null),[LineTotal]+(CLng([LineTotal]*[SalesTaxRate]*100)/100)+[FreightCharge]-nz([Total Payments]),0)),2) AS Current_Unshipped,
    Round(Sum(IIf((Date()-[ShipDate])<61 And (Date()-[ShipDate]>30),[LineTotal]+(CLng([LineTotal]*[SalesTaxRate]*100)/100)+[FreightCharge]-nz([Total Payments]),0)),2) AS [31-60 Days], 
    Round(Sum(IIf((Date()-[ShipDate])<91 And (Date()-[ShipDate]>60),[LineTotal]+(CLng([LineTotal]*[SalesTaxRate]*100)/100)+[FreightCharge]-nz([Total Payments]),0)),2) AS [61-90 Days],
    Round(Sum(IIf((Date()-[ShipDate])>90,[LineTotal]+(CLng([LineTotal]*[SalesTaxRate]*100)/100)+[FreightCharge]-nz([Total Payments]),0)),2) AS [91+ Days],
    Round(Sum(nz([LineTotal])+(CLng(nz([LineTotal])*nz([SalesTaxRate])*100)/100)+nz([FreightCharge])-nz([Total Payments])),2) AS Balance,
    [Receivables Aging Report Query].CompanyName, 
    [Receivables Aging Report Query].OrderDate, 
    [Receivables Aging Report Query].ShipDate, 
    [Receivables Aging Report Query].OrderID, 
    nz((Round(Date()-[ShipDate])),"NULL") AS Span, 
FROM [Receivables Aging Report Query]
GROUP BY 
    [Receivables Aging Report Query].CompanyName, 
    [Receivables Aging Report Query].OrderDate, 
    [Receivables Aging Report Query].ShipDate, 
    [Receivables Aging Report Query].OrderID
HAVING(((Round(Sum(nz([LineTotal])+(CLng(nz([LineTotal])*nz([SalesTaxRate])*100)/100)+nz([FreightCharge])-nz([Total Payments])),2))>0 Or (Round(Sum(nz([LineTotal])+(CLng(nz([LineTotal])*nz([SalesTaxRate])*100)/100)+nz([FreightCharge])-nz([Total Payments])),2))<0) AND (([Receivables Aging Report Query].ShipDate) Is Not Null)) OR (((Round(Sum(nz([LineTotal])+(CLng(nz([LineTotal])*nz([SalesTaxRate])*100)/100)+nz([FreightCharge])-nz([Total Payments])),2))>0 Or (Round(Sum(nz([LineTotal])+(CLng(nz([LineTotal])*nz([SalesTaxRate])*100)/100)+nz([FreightCharge])-nz([Total Payments])),2))<0) AND (([Receivables Aging Report Query].ShipDate) Is Null))
ORDER BY 
    [Receivables Aging Report Query].OrderID;

1 个答案:

答案 0 :(得分:0)

使用此:

Round(Sum(IIf(DateDiff("d",Date(),[ShipDate])=0,[LineTotal]+CLng([LineTotal]*[SalesTaxRate]*100)/100+[FreightCharge]-Nz([Total Payments],0),0)),2) AS Current_Unshipped