【问题标题】:VBA connect to SQL Server data source name not found and no default driver specified?未找到 VBA 连接到 SQL Server 数据源名称且未指定默认驱动程序?
【发布时间】:2012-03-20 22:28:09
【问题描述】:

我有以下 VBA 代码,并且收到此问题标题中所述的错误消息。有人可以帮忙吗?我通常不写这种代码:

Sub Save_To_DB()
'Declare some variables
Dim cnn As ADODB.Connection
Dim cmd As ADODB.Command
Dim strSQL As String

'Create a new Connection object
Set cnn = New ADODB.Connection

'Set the connection string
cnn.ConnectionString = "Server=server_name;Database=database_name;Trusted_Connection=True;"

'Open the Connection to the database
cnn.Open **<-THE ERROR THROWS HERE**

'Create a new Command object
Set cmd = New ADODB.Command

'Associate the command with the connection
cmd.ActiveConnection = cnn

'Go on to write the SQL statement and execute

编辑:好的,我解决了,需要在连接字符串中包含Driver={SQL Native Client};

【问题讨论】:

    标签: sql sql-server-2008 excel vba


    【解决方案1】:

    这意味着你的连接字符串错误,在这里寻找正确的http://www.connectionstrings.com/

    如果您很难找到适合您的方法,请使用外部数据向导附加一个表格,然后查看它提供的连接字符串:

    CurrentDB.TableDefs("MyLinkedTable").Connect
    

    【讨论】:

    • 嗨 Remou,我使用了那个网站,但似乎有很多不同的方式来指定它。你能举一个你知道的作品的小例子吗?
    • 我到 SQL 服务器的连接字符串不适合你。您需要了解有关您自己的服务器的信息,在此发布不是一个好主意。不过,我会在我的回答中添加注释。
    猜你喜欢
    • 2022-08-13
    • 1970-01-01
    • 2014-12-05
    • 1970-01-01
    • 1970-01-01
    • 2013-07-24
    相关资源
    最近更新 更多