类型不匹配,打开一个RecordSet

时间:2019-02-18 11:46:40

标签: mysql excel vba

始终得到此错误:

  

运行时错误'13':类型不匹配。

在此行 rs.Open , sqlstr, conn

我尽了一切努力都无法解决问题。我正在尝试更新MySQL数据库中的一行。

 Private Sub CommandButton3_Click()
    Dim conn As New ADODB.Connection
    Dim server_name As String
    Dim database_name As String
    Dim user_id As String
    Dim password As String
    Dim i As Long 
    Dim sqlstr As String 
    Dim table1 As String, table2 As String
    Dim field1 As String, field2 As String
    Dim rs As ADODB.Recordset
    Dim vtype As Variant

    server_name = "99.99.99.99"
    database_name = "name" 
    user_id = "user" 
    password = "pass"

    Set conn = New ADODB.Connection
    conn.Open "DRIVER={MySQL ODBC 3.51 Driver}" _
    & ";SERVER=" & server_name _
    & ";DATABASE=" & database_name _
    & ";UID=" & user_id _
    & ";PWD=" & password _
    & ";OPTION=16427"

    Set rs = New ADODB.Recordset
    sqlstr = "SET SQL_SAFE_UPDATES = 0; UPDATE table SET poslano = 'Poslano' WHERE poslano = 'Neposlano';"

    rs.Open , sqlstr, conn
    With Worksheets("List6").Cells("A1") 
    .ClearContents
    .CopyFromRecordset rs
    End With
    skipextract:

    On Error Resume Next
    rs.Close
    Set rs = Nothing
    conn.Close
    Set conn = Nothing
    On Error GoTo 0

    End Sub

1 个答案:

答案 0 :(得分:0)

rs.Open之后的逗号使VBA“认为”省略了源代码:

enter image description here

因此,它给出类型不匹配错误。尝试这样写:

rs.Open sqlstr, conn