简体   繁体   English

无法使用VBA连接到SQL Server(运行时错误(80040e14))

[英]Cannot connect to sql server with VBA (Run-time error (80040e14))

I am trying to connect to my Sql database and get one simple recordset out of it running this code: 我正在尝试连接到我的Sql数据库,并从中获取一个简单的记录集来运行此代码:

    Sub ConnectToSQL()

    Dim cnn As New ADODB.Connection
    Dim cmd As New ADODB.Command
    Dim rs As New ADODB.Recordset

    With cnn
         .ConnectionString = "File Name=C:\inetpub\tc\TEST.udl"
         .Open
    End With

    With cmd
          .ActiveConnection = cnn
          .CommandType = xlCmdSql
          .CommandText = "SELECT * FROM TEST"
    End With

    rs.Open cmd.Execute, cnn

    If Not rs.EOF Then
           Sheets(1).Range("A1").CopyFromRecordset rs
           rs.Close
    Else
           MsgBox "No records returned", vbCritical
    End If

    cn.Close
    Set cn = Nothing
    Set rs = Nothing

    End Sub

As a result I get the run-time error message (80040e14) What could be wrong here? 结果,我收到运行时错误消息(80040e14)这里可能出什么问题?

Thanks, 谢谢,

Do you want to try following? 您要尝试追踪吗?
Please do not forget to add Tools> Refernces if necessary. 如有必要,请不要忘记添加工具>引用。

Sub ConnectToSQL()    
    Dim cnn As ADODB.Connection
    Dim rs As New ADODB.Recordset
    Dim ConnectionString As String

     Set cnn = New ADODB.Connection

    ConnectionString = "File Name=C:\inetpub\tc\TEST.udl"

    cnn.Open ConnectionString
    cnn.CommandTimeout = 900

     strquery = "SELECT * FROM TEST;"

    'MsgBox strquery
    rs.Open strquery, cnn

    If Not rs.EOF Then
           Sheets(1).Range("A1").CopyFromRecordset rs
           rs.Close
    Else
           MsgBox "No records returned", vbCritical
    End If

    cnn.Close
    Set cnn = Nothing
    Set rs = Nothing
End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试使用VBA从SQL Server检索数据时出现运行时错误'-2147217900(80040e14) - Run-time error '-2147217900 (80040e14) while trying to retrieving data from SQL Server using VBA 运行时错误“-2147217900 (80040e14)”:“1”附近的语法不正确 - Run-time error' -2147217900 (80040e14)': Incorrect syntax near '1' SQL Server错误'80040e14'列名不明确 - SQL Server error '80040e14' Ambiguous column name 尝试从 SQL Server 检索数据时出现 VBA ADODB 错误 (80040e14) - VBA ADODB error (80040e14) when trying to retrieve data from SQL Server SQL Server 2012 上的 SQL Server 错误“80040e14”语法不正确 - SQL Server error '80040e14' incorrect syntax on SQL Server 2012 SQL Server的Microsoft OLE DB提供程序错误'80040e14'无效的列名。 SQL查询 - Microsoft OLE DB Provider for SQL Server error '80040e14' Invalid column name . SQL query Microsoft OLE DB提供程序的SQL Server错误'80040e14'找不到存储的过程 - Microsoft OLE DB Provider for SQL Server error '80040e14' Could not find stored procedure Microsoft OLE DB提供程序的SQL Server错误'80040e14' - Microsoft OLE DB Provider for SQL Server error '80040e14' 用于SQL Server的Microsoft OLE DB提供程序错误“80040e14”'='附近的语法不正确 - Microsoft OLE DB Provider for SQL Server error '80040e14' Incorrect syntax near '=' SQL Server错误80040e14在结果集中使用经典ASP - SQL Server error 80040e14 Using Classic ASP in a Result Set
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM