通过ADO使用SQL Update更新Excel数据

时间:2015-03-24 14:15:04

标签: sql excel vba ado

此代码的目的是更新表中过滤器的名称。该表有三列(Filtername,location,attributes)。 始终使用Excel中的以下代码获取错误(需要更多参数):

Sub NewFilterName()
Dim FilternameOld As String
Dim FilternameNew As String
    'chose Filter
    FilternameOld = "abc"
    'Inputbox for new Filtername
    FilternameNew = InputBox("Bitte geben Sie einen neuen Filternamen ein:", "Filternamen festlegen", FilternameOld) 'The variable is assigned the value entered in the InputBox
    If FilternameNew <> "" Then  'If the value anything but "" the result is displayed
        closeRS
        OpenDB
        strSQL = "UPDATE [Filter$] SET Filtername=" & FilternameNew & " WHERE Filtername=" & FilternameOld
        Debug.Print strSQL
        Set rs = cnn.Execute(strSQL)
    Else
        MsgBox ("Bitte geben Sie einen Filternamen ein!")
    End If
    closeRS
End Sub

'open database - this workbook
Public Sub OpenDB()
    If cnn.State = adStateOpen Then cnn.Close
    cnn.ConnectionString = "Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=" & _
    ActiveWorkbook.Path & Application.PathSeparator & ActiveWorkbook.Name & ";ReadOnly=False"
    cnn.Open
End Sub

'close Recordset
Public Sub closeRS()
    If rs.State = adStateOpen Then rs.Close
    rs.CursorLocation = adUseClient
End Sub

任何帮助都表示赞赏,当然可以随意改进我的代码。

0 个答案:

没有答案