VBA宏更新数据连接。连接字段不接受变量

时间:2017-01-18 16:25:41

标签: excel vba excel-vba

我正在编写一个宏,要求用户输入更新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

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

1 个答案:

答案 0 :(得分:0)