从VBA Excel导出到Access时语法错误(缺少运算符)

时间:2017-10-10 02:09:44

标签: excel vba excel-vba ms-access

我正在尝试将我的代码输出从excel电子表格输出到访问数据库,但是代码总是在查询表达式“Road Cost”中导致语法错误(缺少表达式)。它应该循环选择单元格并将其复制到数据库中。

Sub export()
'
' export Macro
Dim rs As Object
Dim cn As Object
Dim SQL As String
Dim columncount As Integer
Dim Value, id As Integer
Dim Criteria As String
Dim haha As Range

Set cn = CreateObject("ADODB.connection")
accessfile = ThisWorkbook.Path & "\Engi340 P1.accdb"
cn.Open "provider=Microsoft.Ace.OLEDB.12.0; Data source =" & accessfile
Set rs = CreateObject("ADODB.Recordset")
Set haha = Range("J21")
id = 1
Do While id < 4
 hoho = haha.Offset(id - 1, 0)
 hehe = haha.Offset(id - 1, 1)
 SQL = "UPDATE Results SET Criteria=" & hoho & ", Value=" & hehe & " WHERE ID=" & id & ";"
 Set rs = cn.Execute(SQL)
 id = id + 1
 Loop
 End Sub

更新:在访问数据库中,ID是一个数字,条件是一个短文本,值是一个数字。这两个数字都是长整数。

0 个答案:

没有答案