nyconfidential
New Member
- Joined
- Jul 22, 2015
- Messages
- 49
- Office Version
- 365
- 2016
Hi all - I'm accessing a SQL Server database via an ADO connection in VBA. I can retrieve records, import them into the Excel spreadsheet, the connection seems to be fine. However, when I try to update the records in the SQL database from VBA via ADO connection, I receive the following error message: "Run Time error: '-2147217872 (80040e2f) Automation error"
The error occurs on the "rs.update" line below - anyone know what I might be doing wrong? Note - I've updating all fields in the table, received the same error, just updating one field now for demonstrative purposes. Appreciate the help, thanks!
The error occurs on the "rs.update" line below - anyone know what I might be doing wrong? Note - I've updating all fields in the table, received the same error, just updating one field now for demonstrative purposes. Appreciate the help, thanks!
VBA Code:
Sub ConnectSqlServer()
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim sConnString As String
' Create the connection string.
sConnString = "Driver={ODBC Driver 17 for SQL Server};Server=OurServer;Database=OurDB;UID=OurUID;PWD=OurPW;"
' Create the Connection and Recordset objects.
Set conn = New ADODB.Connection
Set rs = New ADODB.Recordset
' Open the connection and execute.
conn.Open sConnString
rs.Open "SELECT * FROM tm_endts;", conn, adOpenKeyset, adLockOptimistic
'Set rs = conn.Execute("SELECT * FROM tm_endts;")
rs.AddNew
rs.Fields(0) = "Test"
rs.Update 'ERROR OCCURS HERE
rs.Close
End Sub