Automation Error upon running VBA script in Excel

I'm getting an Automation error upon running VBA code in Excel 2007. I'm attempting to connect to a remote SQL Server DB and load data to from Excel to SQL Server.

The error I get is,

"Run-time error '-2147217843(80040e4d)': Automation error".

I checked out the MSDN site and it suggested that this may be due to a bug associated with the sqloledb provider and one way to mitigate this is to use ODBC. Well I changed the connection string to reflect ODBC provider and associated parameters and I'm still getting the same error.

Here is the code with ODBC as the provider:

Dim cnt As ADODB.Connection
Dim rst As ADODB.Recordset
Dim stSQL As String
Dim wbBook As Workbook
Dim wsSheet As Worksheet
Dim rnStart As Range

Public Sub loadData()
     'This was set up using Microsoft  ActiveX Data Components version 6.0.

     'Create ADODB connection object, open connection and construct the connection string object.
     Set cnt = New ADODB.Connection
     cnt.ConnectionString = _
     "Driver={SQL Server};; Database=fooDB Uid=logonalready;Pwd='helpmeOB1';"


    On Error GoTo ErrorHandler

    'Open Excel and run query to export data to SQL Server.

     strSQL = "SELECT * INTO SalesOrders FROM OPENDATASOURCE('Microsoft.ACE.OLEDB.12.0',   & _
    "'Data Source=C:\Database.xlsx; Extended Properties=Excel 12.0')...[SalesOrders$]"

    cnt.Execute (strSQL)

    'Error handling.
     'Reclaim memory from the connection objects
     Set rst = Nothing
     Set cnt = Nothing

   Exit Sub

   MsgBox Err.Description, vbCritical
   Resume ErrorExit

   'clean up and reclaim memory resources.
    If CBool(cnt.State And adStateOpen) Then
    Set rst = Nothing
    Set cnt = Nothing

    End If

End Sub


From the replies on your post (and yours) it seems your connectionstring is faulty? Connection strings can cause a real headache. I know. Try Robert Gelb's approach: Works for me every time.

Need Your Help

When using DialogBoxIndirect, how do I get text the user entered when the dialog closes?

c++ c winapi dialog modal-dialog

I'm using DialogBoxIndirect() to create a modal dialog in memory. One of the controls that I'm adding to the dialog has the EDIT class, so users can type in information in the dialog. When the di...

How to find last day of week in iPhone?


In my application I'm using following codes to retrieve current date and day :-