Access 2010 中的 SQL 错误

Posted

技术标签:

【中文标题】Access 2010 中的 SQL 错误【英文标题】:SQL error in Access 2010 【发布时间】:2014-10-06 09:49:10 【问题描述】:

我在 MSAccess 2010 中创建了一个表单,运行时出现以下错误: select 语句包含保留字或参数名称拼写错误或缺失,或标点符号不正确

我的数据库名称是 Accessexp

这是我的代码:

   Private Sub SearchButton_Click()


    Dim dbs As Database
    Dim rs As Recordset
    Dim qdf As QueryDef
    Dim productName As String
    Dim lastNQuarters As String
    Dim strSql As String
    Dim columns As String
    Dim compstr As String
    Dim curstr As String
    Dim timestr As String
    Dim tablestr As String
    Dim sumstr As String
    Dim varItem As Variant
    Dim compcount As Integer
    Dim colcount As Integer
    Dim errorcount As Integer
    Dim Monthvalue As String


    Set dbs = CurrentDb()


    errorcount = 0
    colcount = 0
    lastNQuarters = Me!quarterbox.Value

    tablestr = ""
    timestr = Me!Monthbox.Value
    MsgBox (timestr)

    tablestr = "Q" & lastNQuarters


For Each varItem In Me!columnlist.ItemsSelected

     If Me!columnlist.ItemData(varItem) <> "---" Then
    columns = columns & "," & Chr(34) & Me!columnlist.ItemData(varItem) & Chr(34)
     colcount = colcount + 1
    End If
    Next varItem
    compcount = 0
    For Each varItem In Me!practicelist.ItemsSelected
       compstr = compstr & "," & Chr(34) & Me!practicelist.ItemData(varItem) & Chr(34)
         compcount = compcount + 1
    Next varItem

    If compcount = 0 Then MsgBox ("Warning: No Practice Selected"): errorcount = errorcount + 1
    If colcount = 0 Then MsgBox ("Warning: No Practice Selected"): errorcount = errorcount + 1

    If errorcount = 0 Then
          lastNQuarters = Me!quarterbox.Value


strSql = "Select  " & columns & ", " & tablestr & " from Accessexp where Accessexp.[Quarter] = " & tablestr & " and Accessexp.[Month] = " & timestr & ";"
            MsgBox (strSql)

            Set rs = dbs.OpenRecordset(strSql, dbOpenSnapshot)

          With dbs
          Set qdf = .CreateQueryDef("QueryOutput", strSql)
          DoCmd.OpenQuery "QueryOutput"
          .QueryDefs.Delete "QueryOutput"
          End With
          dbs.Close
           qdf.Close

        End If


    End Sub

我的查询是这样的:

Select ,"Revenue Blended","Revenue Direct" Q4
from Accessexp
where Accessexp.[Quarter] = Q4 and Accessexp.[Month] = April;

【问题讨论】:

请从您的Select 语句中删除第一个逗号, @nobodynoone :此语句从 List Box 中一一挑选列值。为了将它们分开,我使用了 columns = columns & "," & Chr(34) & Me!columnlist.ItemData(varItem) & Chr(34) 有没有其他方法可以用逗号分隔值而不是放在开头。 .. 我不确定 MS Access 2010 但肯定有类似 php 的内容。您应该检查您的参数是否不为空,然后在此基础上添加逗号,这样您就不会有额外的字符,而是会“即时”添加它们 【参考方案1】:

有没有其他方法可以用逗号分隔值而不是开头

用前导逗号构建字符串后,只需使用

columns = Mid(columns, 2)

剪掉第一个字符。

其他说明:

    将列名括在方括号中,而不是双引号 字符串文字需要用引号括起来。

生成的查询字符串应该看起来更像这样:

Select [Revenue Blended],[Revenue Direct]
from Accessexp
where Accessexp.[Quarter] = 'Q4' and Accessexp.[Month] = 'April';

【讨论】:

【参考方案2】:

试试这个更新的代码。

Private Sub SearchButton_Click()
    Dim dbs As Database, rs As Recordset
    Dim qdf As QueryDef
    Dim productName As String, lastNQuarters As String
    Dim strSql As String, columns As String, compstr As String
    Dim curstr As String, timestr As String, tablestr As String
    Dim sumstr As String, varItem As Variant
    Dim compcount As Integer, colcount As Integer, errorcount As Integer
    Dim Monthvalue As String

    Set dbs = CurrentDb()

    errorcount = 0
    colcount = 0
    lastNQuarters = Me!quarterbox.Value

    tablestr = ""
    timestr = Me!Monthbox.Value
    MsgBox timestr

    tablestr = "Q" & lastNQuarters

    For Each varItem In Me!columnlist.ItemsSelected
        If Me!columnlist.ItemData(varItem) <> "---" Then
            columns = columns & "[" & Me!columnlist.ItemData(varItem) & "],"
            colcount = colcount + 1
        End If
    Next 

    compcount = 0

    For Each varItem In Me!practicelist.ItemsSelected
        compstr = compstr & "[" & Me!practicelist.ItemData(varItem) & "],"
        compcount = compcount + 1
    Next

    If compcount = 0 Then 
        MsgBox ("Warning: No Practice Selected")
        errorcount = errorcount + 1
    Else
        compstr = Left(compstr, Len(compstr)-1)
    End If

    If colcount = 0 Then 
        MsgBox ("Warning: No Practice Selected")
        errorcount = errorcount + 1
    Else
        columns = Left(columns, Len(columns)-1)
    End If

    If errorcount = 0 Then
        lastNQuarters = Me!quarterbox.Value
        strSql = "SELECT  " & columns & " FROM Accessexp WHERE Accessexp.[Quarter] = '" & tablestr & "'" & _
                 " AND Accessexp.[Month] = '" & timestr & "';"
        MsgBox (strSql)
        Set rs = dbs.OpenRecordset(strSql, dbOpenSnapshot)

        With dbs                
            Set qdf = .CreateQueryDef("QueryOutput", strSql)
            DoCmd.OpenQuery "QueryOutput"
            '.QueryDefs.Delete "QueryOutput"
        End With
        dbs.Close
        qdf.Close
    End If
End Sub

【讨论】:

:它第一次运行,但下一次它总是给出这个错误:运行时错误'3012':对象'QueryOutput'已经存在。指向这一行: Set qdf = .CreateQueryDef("QueryOutput", strSql) – 谢谢我得到了解决方案。您的代码正在运行。取消注释 '.QueryDefs.Delete "QueryOutput" 并将其放在 Set qdf = .CreateQueryDef("QueryOutput", strSql) 之前

以上是关于Access 2010 中的 SQL 错误的主要内容,如果未能解决你的问题,请参考以下文章

Access 2010 中的 INSERT INTO 语句中出现 SQL 语法错误

Access 2010:查询表达式中的语法错误(缺少运算符)

Access 2010:加入三个表,未知错误

Access 2010 和 SQL Server 2008 R2 错误 3157 超时

在 SQL Server 2012 中作为 Varbinary(MAX) 的照片在 Access 2010 中导致错误 502753

如何从 Access 2010 中的 SQL 存储过程返回多个记录集