[英]Automation Error upon running VBA script in Excel
我在Excel 2007中運行VBA代碼時遇到自動化錯誤。我正在嘗試連接到遠程SQL Server數據庫並將數據從Excel加載到SQL Server。
我得到的錯誤是,
“運行時錯誤'-2147217843(80040e4d)':自動化錯誤”。
我檢查了MSDN網站,它表明這可能是由於與sqloledb提供程序相關的錯誤,緩解這種情況的一種方法是使用ODBC。 好吧,我更改了連接字符串以反映ODBC提供程序和相關參數,我仍然得到相同的錯誤。
以下是使用ODBC作為提供程序的代碼:
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}; Server=onlineSQLServer2010.foo.com; Database=fooDB Uid=logonalready;Pwd='helpmeOB1';"
cnt.Open
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.
ErrorExit:
'Reclaim memory from the connection objects
Set rst = Nothing
Set cnt = Nothing
Exit Sub
ErrorHandler:
MsgBox Err.Description, vbCritical
Resume ErrorExit
'clean up and reclaim memory resources.
cnt.Close
If CBool(cnt.State And adStateOpen) Then
Set rst = Nothing
Set cnt = Nothing
End If
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.