我在Excel中创建一个按钮,该按钮应该根据在特定Excel工作表上输入的少量参数来更改连接字符串(到SQL Server)。我遇到过这个链接 Excel macro to change external data query connections - e.g. point from one database to another
我总结了以下内容,但它无法正常工作
Sub GetConnections()
Dim ConnectionString
ConnectionString = "DRIVER=SQL Server;SERVER=.;UID=sa;PWD=123;APP=Microsoft Office 2013;WSID=MachineID;DATABASE=X"
Dim w As Worksheet
Dim cn As WorkbookConnection
Dim OdbcCn As ODBCConnection
For Each cn In ThisWorkbook.Connections
Set OdbcCn = cn.ODBCConnection
OdbcCn.SavePassword = True
OdbcCn.Connection = ConnectionString
Next
MsgBox ("Done")
End Sub
当我运行上面的代码时,我明白了 运行时错误(1004): 应用程序定义或对象定义的错误
调试器突出显示“OdbcCn.Connection = ConnectionString”
非常感谢您的帮助,