I am trying to create a VBA code in Excel 2010 which deletes all the rows from the table 'Info_Table' in SQL, but when I run the macro, I am getting the following error message: "Run-time error '-2147467259 (80004005)': Could not delete from specified tables." When I click on the Debug button, the line 'cn.Execute strSQL, lngRecsAff' is highighted. Any help would be appreciated. My code is as follows:
Sub trans()
Dim cn As ADODB.Connection
Dim strSQL As String
Dim lngRecsAff As Long
Dim sfilename As String
sfilename = ActiveWorkbook.FullName
Set cn = Nothing
Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & sfilename & ";" & _
"Extended Properties=Excel 8.0;"
strSQL = "DELETE FROM [odbc;Driver={SQL Server};" & _
"Server=cowboys;Database=stats;" & _
"Trusted_Connection = True].Info_Table"
cn.Execute strSQL, lngRecsAff
Debug.Print "Records affected: " & lngRecsAff
MsgBox "Records Submitted: " & lngRecsAff
cn.Close
Set cn = Nothing
End Sub
Thanks!
Sub trans()
Dim cn As ADODB.Connection
Dim strSQL As String
Dim lngRecsAff As Long
Dim sfilename As String
sfilename = ActiveWorkbook.FullName
Set cn = Nothing
Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & sfilename & ";" & _
"Extended Properties=Excel 8.0;"
strSQL = "DELETE FROM [odbc;Driver={SQL Server};" & _
"Server=cowboys;Database=stats;" & _
"Trusted_Connection = True].Info_Table"
cn.Execute strSQL, lngRecsAff
Debug.Print "Records affected: " & lngRecsAff
MsgBox "Records Submitted: " & lngRecsAff
cn.Close
Set cn = Nothing
End Sub
Thanks!