1

我正在尝试使用以下方法从 excel vba 连接到我的 sqlserver express 2005(从一些论坛例程复制,因为我不是很喜欢数据库)。它工作完美。一旦我在 sqlquery 变量中使用 WHERE(已经验证过双引号和单引号),我就会得到

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

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

谢谢

子连接测试()

' 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

结束子

4

0 回答 0