sql vba查询表达式语法错误

时间:2016-05-18 11:52:36

标签: vba access

我试图通过访问vba sq来运行以下内容,但在查询表达式' 0)'"中出现错误" Extra)。以下是查询表达式:

mnthfld = "SELECT IIf([issuedate] <= #" & useDateUpper & "#," & _
    "IIf([expirydate] <= #" & useDateUpper & "#, 0, IIf([expirydate <=#" & useDateUpper & "#," & _
    "([expirydate]-#" & useDateUpper & "#+1)/([expirydate]-[effectivedate]+1),[grosspremium]),0),0) as 'EP M" & _
    Format(useDateLower, "mm yyyy") & "' FROM tblEpData"
  

感谢任何帮助。我的预感是我在减法和除法部分错误地使用了括号。

更新:

一次迭代时mnthfield字符串的快照:

IIf([issuedate] <= #11/30/2015#,IIf([expirydate] <= #11/30/2015#, 0, IIf([expirydate <=#11/30/2015#,([expirydate]-#11/30/2015#+1)/([expirydate]-[effectivedate]+1),[grosspremium]),0,0)) as [EP M11 2015]

2 个答案:

答案 0 :(得分:1)

每个IIf实例需要3个参数(bool_test,value_if_true,value_if_false)

我已将下面的字符串转换为语法正确;您需要对其进行审核,以确保它符合您的预期!

IIf([issuedate]<= #11/30/2015#,IIf([expirydate] <= #11/30/2015#, 0, IIf([expirydate] <=#11/30/2015#,(([expirydate]-#11/30/2015#+1)/([expirydate]-[effectivedate]+1)),[grosspremium])),0) as [EP M11 2015]

答案 1 :(得分:1)

在语句]中添加[expirydate <=#" & useDateUpper,然后从,0中移除[grosspremium]),0即可。最终查询如下:

SELECT 
IIf
(
    [issuedate] <= #11/30/2015#, 
    IIf
    (
        [expirydate] <= #11/30/2015#, 
        0, 
        IIf
        (
            [expirydate] <=#11/30/2015#,
            ([expirydate]-#"11/30/2015"#+1)/([expirydate]-[effectivedate]+1),
            [grosspremium]
        )
    ),
    0
) as [EP M11 2015]