I need to transfer 5 times per second excel columns to an mssql server. I want to overwrite the existing data in the sql server. I have tried a lot of code on the internet but it will not work. I have a working code to read a sql table to a worksheet in excel. Is it possible to write the data to the sql server? I have not much experience with vba or a sql server. I hope someone can help with the code.
I have post my working code to read the data out of the table in the SQL server, I have have deleted the password. Sorry for bad English.
Sub ADOExcelSQLServer()
' Carl SQL Server Connection
'
' FOR THIS CODE TO WORK
' In VBE you need to go Tools References and check Microsoft Active X Data Objects 2.x library
Dim cn As ADODB.Connection
Dim Server_Name As String
Dim Database_Name As String
Dim User_ID As String
Dim Password As String
Dim SQLStr As String
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
Server_Name = "NLDONL0113" ' Enter your server name here
Database_Name = "Stroomwaarden" ' Enter your database name here
User_ID = "Admin" ' enter your user ID here
Password = "" ' Enter your password here
SQLStr = "SELECT * FROM [Stroomwaarde]" ' Enter your SQL here"
Set cn = New ADODB.Connection
cn.Open "Driver={SQL Server};Server=" & Server_Name & ";Database=" & Database_Name & _
";Uid=" & User_ID & ";Pwd=" & Password & ";"
rs.Open SQLStr, cn, adOpenStatic
' Dump to spreadsheet
With Worksheets("sheet1").Range("M3:M500") ' Enter your sheet name and range here
.ClearContents
.CopyFromRecordset rs
End With
' Tidy up
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
End Sub