【问题标题】:VBA Syntax error (missing operator) in query expression 'PopID ='查询表达式“PopID =”中的 VBA 语法错误(缺少运算符)
【发布时间】:2013-08-13 13:18:43
【问题描述】:

以下代码在尝试运行时会引发错误,我想我已经设法实际连接到数据库并且我选择了一个单元格,所以不确定缺少什么。

错误:

查询表达式“PopID =”中有语法错误(缺少运算符)。

理想情况下,我希望能够在每次运行宏时列出四个单元格,这些单元格将进入四列访问附加

Const TARGET_DB = "testdb.accdb"

Sub AlterOneRecord() 'not working yet

   Dim cnn As ADODB.Connection
   Dim rst As ADODB.Recordset
   Dim fld As ADODB.Field
   Dim MyConn
   Dim lngRow As Long
   Dim lngID As String
   Dim j As Long
   Dim sSQL As String

                   'determine the ID of the current record and define the SQL statement
                   lngRow = ActiveCell.Row
                   lngID = Cells(lngRow, 1).Value

   sSQL = "SELECT * FROM tblPopulation WHERE PopID = " & lngID

   Set cnn = New ADODB.Connection
   MyConn = ThisWorkbook.path & Application.PathSeparator & TARGET_DB

   With cnn
     .ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;"
     .Open MyConn
   End With

   Set rst = New ADODB.Recordset
   rst.CursorLocation = adUseServer
   rst.Open Source:=sSQL, _
            ActiveConnection:=cnn, _
            CursorType:=adOpenKeyset, _
            LockType:=adLockOptimistic

   'Load contents of modified record from Excel to Access.
   'do not load the ID again.
   For j = 2 To 7
      rst(Cells(1, j).Value) = Cells(lngRow, j).Value
   Next j
   rst.Update

   ' Close the connection
   rst.Close
   cnn.Close
   Set rst = Nothing
   Set cnn = Nothing
End Sub

我觉得奇怪的是它们都是 M$ 产品,没有很好的文档记录或者真的很容易执行。也许我会以错误的方式去做。

例如,我怎样才能让它包含单元格 A1 和 B2?

【问题讨论】:

    标签: sql excel vba ms-access


    【解决方案1】:

    你需要引用字符串

    sSQL = "SELECT * FROM tblPopulation WHERE PopID = '" & lngID & "'"
    

    【讨论】:

    • "no value given for more or one parameters"错误现在出现
    • 您的lngID 似乎不包含任何值。
    • 如何让它包含例如单元格 A1 和 B2?
    • 不知道。我不是 VBA 导出。
    猜你喜欢
    • 2021-06-18
    • 2021-03-14
    • 2020-01-14
    • 1970-01-01
    • 2018-09-02
    • 2018-01-29
    • 2012-11-14
    相关资源
    最近更新 更多