使用Excel VBA通​​过ADODB更新SQL Server中的数据

时间:2017-02-11 05:20:15

标签: sql excel excel-vba excel-formula vba

在Excel Sheet1 中,数据从SQL Server中提取:

code   Name
1      ALI
2      SALEM

我希望vba的更新数据不是来自完整的SQL查询  将姓名ALI替换为Ahmed

有办法做到这一点吗? Excel是否为我提供了一种在不发送SQL查询的情况下进行更新的方法?

一些信息:

**TABEL Name** = Tb1
**colmun1** = code(in excel)   = Emp_code (sql)
**colmun2** = Name(in excel)   = E_name   (sql)

服务器名称:HHsqlexpress

1 个答案:

答案 0 :(得分:0)

我通过ADODB代码>>>找到了这个VBA如果有任何修改或添加

Private Sub UpdateSQLData()
On Error GoTo err
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim sql_string As String
Dim strconn As String

DataBaseName = "----"
ServerName = "196.1.1.1/SSDDSDS"
UserId = "ADMIN"
Password = "123"

strconn = "Provider=SQLOLEDB.1;Password=" & Password & ";User ID=" & UserId & ";Initial Catalog=" & DataBaseName & ";Data Source=" & ServerName
sql_string = "update transactions set CustomerID='2'  where amount =1000 "
cn.Open strconn
rs.Open sql_string, cn


Set rs = Nothing
cn.Close

Exit Sub
err:
MsgBox err.Description
End Sub