Delete From VBA Code in Excel

dgilrein

New Member
Joined
Oct 28, 2013
Messages
4
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!
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.

Forum statistics

Threads
1,215,375
Messages
6,124,588
Members
449,174
Latest member
chandan4057

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top