Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 查询表达式';中的VBA语法错误(缺少运算符);PopID=';_Sql_Excel_Vba_Ms Access - Fatal编程技术网

Sql 查询表达式';中的VBA语法错误(缺少运算符);PopID=';

Sql 查询表达式';中的VBA语法错误(缺少运算符);PopID=';,sql,excel,vba,ms-access,Sql,Excel,Vba,Ms Access,下面的代码在尝试运行它时抛出一个错误,我假设我已经成功连接到数据库,并且我选择了一个单元格,因此不确定缺少什么 错误: 查询表达式“PopID=”中出现语法错误(缺少运算符) 理想情况下,我希望能够列出四个单元格,在每次运行宏时,它们将进入access中的四列并追加 Const TARGET_DB = "testdb.accdb" Sub AlterOneRecord() 'not working yet Dim cnn As ADODB.Connection Dim rst A

下面的代码在尝试运行它时抛出一个错误,我假设我已经成功连接到数据库,并且我选择了一个单元格,因此不确定缺少什么

错误:

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

理想情况下,我希望能够列出四个单元格,在每次运行宏时,它们将进入access中的四列并追加

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?

您需要引用字符串

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

您需要引用字符串

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

现在显示的“没有为多个或一个参数给定值”错误看起来您的
lngID
不包含任何值。如何使其包含单元格A1和B2?不知道。我不是VBA导出。现在显示的“没有为多个或一个参数给定值”错误看起来您的
lngID
不包含任何值。我如何使其包含单元格A1和B2?不知道。我不是VBA出口。