Excel vba update sql server table?

Excel vba update sql server table?

Author
Discussion

aclivity

Original Poster:

4,072 posts

188 months

Thursday 26th April 2018
quotequote all
I believe it IS possible for an excel vba script to update an sql server table - using vba update updateflow or some such?

Does anyone have a simple example of code that I can copy?

Du1point8

21,606 posts

192 months

Thursday 26th April 2018
quotequote all
Sub UpdateTable()
Dim cnn As ADODB.Connection
Dim uSQL As String
Dim rngName As Range
Set cnn = New Connection
cnnstr = "Provider=SQLOLEDB; " & _
"Data Source=MyServer; " & _
"Initial Catalog=Mydb;" & _
"User ID=User;" & _
"Password=Pwd;" & _
"Trusted_Connection=No"
Set rngName = ActiveCell
cnn.Open cnnstr
uSQL = "UPDATE MyTable SET FieldNameX = 1 WHERE FieldNameY= '" & rngName & "' "
'Debug.Print (uSQL)
cnn.Execute uSQL
cnn.Close
Set cnn = Nothing
Exit Sub
End Sub