Select语句包括拼写错误或缺失或标点符号不正确的保留字或参数名称

时间:2012-02-16 15:02:40

标签: sql ms-access vba

我不能为我的生活弄清楚为什么我会收到这个错误。我对VB和SQL非常熟悉,但我不习惯使用Access。我只是在访问中单独运行我的查询,它工作得很好......下面是我的代码。有什么建议!?!?非常感谢!!!

Private Sub Command18_Click()
Dim db As Database
Dim rs As Recordset
Dim sSQL As String

sSQL = "SELECT Count(*) AS Duration" _
         & "FROM [Project_Duration_Info] " _
         & "WHERE [Project - Consulting Partner] In (Select [CP_Name] from [CP's] " _
                                               & "Where [CP_DDL] = [Forms]![Consulting Partners]![CPs]) " _
        & "AND [Project - Actual Complete Date] >= [Forms]![Consulting Partners]![FromDate] " _
        & "AND  [Project - Actual Complete Date] < [Forms]![Consulting Partners]![ToDate]"

Set db = CurrentDb
Set rs = db.OpenRecordset(sSQL)
If rs.RecordCount > 0 Then
Me.Duration = rs!Durations
Else
Me.Duration = ""
End If
Set rs = Nothing
Set db = Nothing
End Sub

1 个答案:

答案 0 :(得分:0)

看起来您正在将VBA和SQL组合成一个语句:[Forms]![Consulting Partners]![CPs]似乎是从表单中提取但嵌入在SQL中的变量

WHERE [CP_DDL] = '" & [Forms]![Consulting Partners]![CPs] & "'

同样的逻辑需要应用于[Forms]![Consulting Partners]![ToDate][Forms]![Consulting Partners]![FromDate],如果这些是日期,您可能需要用#围绕它们(因此最终结果将是#01 / 01/2012#