运行时错误'-2147217900'(80040e14)':自动化错误使用excel vba的sql查询中的位置

时间:2013-05-22 20:49:44

标签: sql excel vba

我正在尝试使用以下方法从excel vba连接到我的sqlserver express 2005(从一些论坛例程复制,因为我不是很熟悉数据库)。它完美无缺。一旦我在sqlquery变量中使用WHERE(已经验证的引号为double和single),我就得到了

运行时错误'-2147217900'(80040e14)': 自动化错误

类似的例程,工作从其他人那里拉取数据。我非常感谢你的帮助

由于

Sub ConnectTEST()

' Create a connection object.
Dim cnPubs As ADODB.Connection
Set cnPubs = New ADODB.Connection
Dim sqlquery As String
Dim rsPubs As ADODB.Recordset
Set rsPubs = New ADODB.Recordset

' Provide the connection string.
Dim strConn As String

'Use the SQL Server OLE DB Provider.
strConn = "PROVIDER=SQLOLEDB;"

sqlquery = "SELECT * FROM [Logisuite].[dbo].[EoWebStatus]"

'Connect to the Pubs database on the local server.
strConn = strConn & "Server=SERVITRANSMAIn\SQLEXPRESS;Database=logisuite;Trusted_Connection=True;"

'Use an integrated login.
strConn = strConn & "INTEGRATED SECURITY=sspi;"

'Now open the connection.
cnPubs.Open strConn

' Create a recordset object.
Set rsPubs = New ADODB.Recordset

With rsPubs
    ' Assign the Connection object.
    .ActiveConnection = cnPubs
    ' Extract the required records.
    .Open sqlquery
    ' Copy the records into cell A1 on Sheet1.
    Sheet1.Range("A1").CopyFromRecordset rsPubs

    ' Tidy up
    .Close
End With

cnPubs.Close
Set rsPubs = Nothing
Set cnPubs = Nothing

End Sub

0 个答案:

没有答案