未找到VBA连接到SQL Server的数据源名称,也未指定默认驱动程序



我有以下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};

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

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

CurrentDB.TableDefs("MyLinkedTable").Connect

相关内容

最新更新