2012-03-02 164 views
2

我有以下的VBA代码,我收到了这个问题标题中指出的错误信息。任何人都可以帮忙吗?我通常不会写这种代码:VBA连接到SQL Server数据源名称未找到并且没有指定默认驱动程序?

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};需要。

回答

2

这意味着您的连接字符串是错误的,请在此处查找正确的一个http://www.connectionstrings.com/

如果你在寻找什么会为你工作的困难,使用外部数据向导附加表,然后看看连接字符串,它提供:

CurrentDB.TableDefs("MyLinkedTable").Connect 
+0

嗨Remou,我使用的网站,但有似乎有很多不同的方式来指定它。你能举一个你知道有效的例子吗? – mezamorphic 2012-03-02 12:10:12

+0

我的连接字符串到SQL服务器不会为你工作。你需要知道关于你自己的服务器的事情,在这里发布它不是一个好主意。但是,我会在我的答案中添加一个注释。 – Fionnuala 2012-03-02 12:15:35

相关问题