ADODB插入另一张纸

时间:2017-04-19 15:00:30

标签: sql excel vba excel-vba

我正在尝试使用SQL插入命令在另一个excel表上创建记录我可以使用查询进行选择,但我不知道如何插入记录我的代码是:

Function database_add(Urun_barkodu, Urun_kodu, Urun_adi, Urun_kategori) As String

  Dim cn As Object, rs As Object, output As String, sql As String, Insert As String
  Dim add_data As String
  Dim rst As ADODB.Recordset
  '---Connecting to the Data Source---
  Set cn = CreateObject("ADODB.Connection")
  Set rst = New ADODB.Recordset
  With cn
    .Provider = "Microsoft.ACE.OLEDB.12.0"
    .ConnectionString = "Data Source=" & ThisWorkbook.Path & "\" & ThisWorkbook.Name & ";" & _
    "Extended Properties=""Excel 12.0 Xml;HDR=YES"";"
    .Open
  End With
  add_data = "INSERT INTO [ürünler$] (columns(1),columns(2),columns(3),columns(4))"
  add_data = add_data & "VALUES (Urun_barkodu, Urun_kodu, Urun_adi, Urun_kategori)"
  cn.Close
  cn.Open
  rst.Open add_data, cn, dOpenStatic, adLockReadOnly, adCmdText
  '---Clean up---
  rs.Close
  cn.Close
  Set cn = Nothing
  Set rs = Nothing
End Function

它给我一个错误:

  INSERT INTO STATEMENT中的

语法错误

1 个答案:

答案 0 :(得分:1)

我猜测 Urun_barkodu,Urun_kodu,Urun_adi Urun_kategori 是字符串变量。您需要将它们连接到字符串中。

add_data = "INSERT INTO [ürünler$] (columns(1),columns(2),columns(3),columns(4)) "
add_data = add_data & "VALUES ('" & Urun_barkodu & "', '" & Urun_kodu& "', '" & Urun_adi & "', '" & Urun_kategori & "');"