Access VBA中的StrSQL错误

时间:2015-12-17 15:26:31

标签: excel vba ms-access

我有一个Access VBA子程序,如下所示:

  Sub SampleReadCurve()

    Dim rs As Recordset
    Dim iRow As Long, iField As Long
    Dim strSQL As String

    strSQL = "SELECT * FROM dbo_VolatilityInput5" & "ORDER BY MaturityDate"
    Set rs = CurrentDb.OpenRecordset(strSQL, Type:=dbOpenDynaset, Options:=dbSeeChanges)



    If rs.RecordCount <> 0 Then

    Do While Not rs.EOF

        Dim BucketTermAmt As Long
        Dim BucketTermUnit As String
        Dim BucketDate As Date
        Dim MarkAsOfDate As Date
        Dim InterpRate As Double
        MarkAsOfDate = rs!MarkAsOfDate
        BucketTermAmt = 3
        BucketTermUnit = "m"
        BucketDate = DateAdd(BucketTermUnit, BucketTermAmt, MarkAsOfDate)
        InterpRate = CurveInterpolateRecordset(rs, BucketDate)
        Debug.Print BucketDate, InterpRate



   rs.MoveNext

Loop

   End If

End Sub

dbo_Volatility5是Access数据库中的一个表。

但是,当我执行此代码时,它为strSQL语句提供了运行时错误“FROM子句中的语法错误”。到目前为止,我可以告诉strSQL语法是正确的。我做错了什么?

2 个答案:

答案 0 :(得分:3)

你在dbo_VolatilityInput5之后错过了一个空格。连接多个字符串以形成SQL查询时,常见的错误。

我更愿意将每个部分放在新的一行上:

const_cast

答案 1 :(得分:1)

以这种方式纠正你的代码:

strSQL = "SELECT * FROM dbo_VolatilityInput5 ORDER BY MaturityDate"

没有必要连接你的字符串,因为它没有任何变量。 如果是这样,就像WHERE子句一样,执行如下操作:

strSQL = "SELECT * FROM dbo_VolatilityInput5 " & some_variable & " ORDER BY MaturityDate"

看到引号后面的空格。