SQL条件中突然出错

时间:2017-02-09 10:36:54

标签: vba ms-access access-vba ms-access-2010

我使用的是以下SQL - 这很好用

Set rs1 = CurrentDb.OpenRecordset("SELECT Count(Advisors) AS TotalNumber FROM tbl_ComplaintsCoded WHERE Advisors = '" & Forms!frm_Central_Reporting_ops!Text52.Value & "' AND Year([Mail Date]) = " & Me.Text48.Value & " AND month([Mail Date]) =4")

我做了一点改动:

Set rs1 = CurrentDb.OpenRecordset("SELECT Count(PolicyComplaints) AS TotalNumber FROM tbl_ComplaintsCoded WHERE PolicyComplaints = '" & Forms!frm_Central_Reporting_ops!Text52.Value & "' AND Year([Mail Date]) = " & Me.Text48.Value & " AND month([Mail Date]) =4")

错误是:

  

表达式中的数据类型不匹配

我无法解决所有出现如此严重错误的事情?

帮助

1 个答案:

答案 0 :(得分:1)

您不能使用单引号括起计算字段。删除引号,它应该工作。这是一个例子:

Set rs1 = CurrentDb.OpenRecordset("SELECT Count(PolicyComplaints) AS TotalNumber FROM tbl_ComplaintsCoded WHERE PolicyComplaints = " & Forms!frm_Central_Reporting_ops!Text52.Value & " AND Year([Mail Date]) = " & Me.Text48.Value & " AND month([Mail Date]) =4")
由于查询参数' 2'以上方法将导致错误输出。当您删除以下引文时,它将起作用:

unexpectedly found nil while unwrapping an Optional value

我已经重写了你的代码而没有引用我的最终报价:

Int