2017-01-18 15 views
1

Excelのデータ接続の接続文字列内のカタログおよびサーバーフィールドを更新するためのユーザー入力を求めるマクロを作成しています。データ接続を更新するVBAマクロ。接続フィールドが変数を受け付けていません

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 

私はに実行している問題は、私はwithステートメント内.Connection = ConnectionStringラインを参照し、「ランタイムエラー1004アプリケーション定義またはオブジェクト定義のエラー」を得ることです。 .ConnectionをConnectionString変数ではなく完全な接続文字列に設定した場合、それは機能します。

答えて

0

接続文字列とその変更は、VBAでは少し問題があるようです。これらの記事をチェックしてください:

Microsoft Excel Data Connections - Alter Connection String through VBA

Excel macro to change external data query connections - e.g. point from one database to another

したがって、おそらく接続のためのMSの例に従うことが良いアイデアです: https://technet.microsoft.com/de-de/library/ee692882.aspx

+1

は、リンクを頂きありがとうございます。私は以前のものを見ていたが、解決策の1つを見逃しているに違いない。リンクの1つで説明した解決策は、変数に格納された接続文字列に 'OLEDB 'を追加することです。これは私の目的のために機能します。再度、感謝します – randyvelour

関連する問題