【问题标题】:VBA macro to update data connection. Connection field not accepting a variable用于更新数据连接的 VBA 宏。连接字段不接受变量
【发布时间】:2017-01-18 16:25:40
【问题描述】:

我正在编写一个宏,它要求用户输入以更新 excel 数据连接的连接字符串中的目录和服务器字段。

Sub Connection_String()

Dim ConnectionString As String
Dim Catalog As String
Dim Server As String

Catalog = InputBox("Catalog", MsgTitle, "")
Server = InputBox("Server", MsgTitle, "")
ConnectionString = "Provider=MSOLAP;Persist Security Info=True;Initial Catalog=" & Catalog & _
";Data Source=" & Server & ";Auto Synch Period=10000;Cache Ratio=1;MDX Compatibility=1;MDX Unique Name Style=2;" & _
"Safety Options=2;MDX Missing Member Mode=Error;Disable Prefetch Facts=True"

With ActiveWorkbook.Connections("Example Connection").OLEDBConnection
    .CommandText = "Example Command Text"
    .CommandType = xlCmdCube
    .Connection = ConnectionString
    .RefreshOnFileOpen = False
    .SavePassword = False
    .SourceConnectionFile = ""
    .MaxDrillthroughRecords = 1000
    .ServerCredentialsMethod = xlCredentialsMethodIntegrated
    .AlwaysUseConnectionFile = False
    .RetrieveInOfficeUILang = True
End With

我遇到的问题是我得到一个“运行时错误 1004 应用程序定义或对象定义错误”,引用 with 语句中的 .Connection = ConnectionString 行。如果我将 .Connection 设置为完整的连接字符串本身,而不是 ConnectionString 变量,它就可以工作。

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    【讨论】:

    • 感谢您的链接。我以前看过那些,但一定错过了其中一个解决方案。其中一个链接中描述的解决方案是将OLEDB 附加到存储在变量中的连接字符串中,它适用于我的目的。再次感谢
    猜你喜欢
    • 2011-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-26
    • 2013-05-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多