title: How can I insert variable database name in recorded Excel-VBA code?
body: <p>The code below is from a recorded macro in Excel 2016 during importing data from an SQL Server Express database.</p>
  
  <pre><code>Sub Macro2()
  
      ActiveWorkbook.Queries.Add Name:="Query1", Formula:= _
          "let" &amp; Chr(13) &amp; "" &amp; Chr(10) &amp; "    Source = Sql.Database(""DESKTOP\SQLEXPRESS"", ""2014-All-1"", [Query=""SELECT *#(lf)  FROM [2014-All-1].[dbo].[OtherData]#(lf)#(lf)  WHERE volume &gt; 1""])" &amp; Chr(13) &amp; "" &amp; Chr(10) &amp; "in" &amp; Chr(13) &amp; "" &amp; Chr(10) &amp; "    Source"
      Sheets.Add After:=ActiveSheet
      With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
          "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Query1" _
          , Destination:=Range("$A$1")).QueryTable
          .CommandType = xlCmdSql
          .CommandText = Array("SELECT * FROM [Query1]")
          .RowNumbers = False
          .FillAdjacentFormulas = False
          .PreserveFormatting = True
          .RefreshOnFileOpen = False
          .BackgroundQuery = True
          .RefreshStyle = xlInsertDeleteCells
          .SavePassword = False
          .SaveData = True
          .AdjustColumnWidth = True
          .RefreshPeriod = 0
          .PreserveColumnInfo = True
          .ListObject.DisplayName = "Query1"
          .Refresh BackgroundQuery:=False
      End With
      Selection.ListObject.QueryTable.Refresh BackgroundQuery:=False
  
  End Sub
  </code></pre>
  
  <p>I have tried to make the database name variable, by adding variable <code>MyDB</code>, which is used twice in the long line of code as you can see. Originally this was the name of the database: 2014-All-1</p>
  
  <p>Unfortunately this method does not work. Does someone know how I can replace the database with a variable name?</p>
  
  <pre><code>Sub Macro2()
  
  MyDB = "2014-All-1"
  
      ActiveWorkbook.Queries.Add Name:="Query1", Formula:= _
          "let" &amp; Chr(13) &amp; "" &amp; Chr(10) &amp; "    Source = Sql.Database(""DESKTOP\SQLEXPRESS"", ""MyDB"", [Query=""SELECT *#(lf)  FROM [MyDB].[dbo].[OtherData]#(lf)#(lf) WHERE volume &gt; 1 ""])" &amp; Chr(13) &amp; "" &amp; Chr(10) &amp; "in" &amp; Chr(13) &amp; "" &amp; Chr(10) &amp; "    Source"
      Sheets.Add After:=ActiveSheet
      With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
          "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Query1" _
          , Destination:=Range("$A$1")).QueryTable
          .CommandType = xlCmdSql
          .CommandText = Array("SELECT * FROM [Query1]")
          .RowNumbers = False
          .FillAdjacentFormulas = False
          .PreserveFormatting = True
          .RefreshOnFileOpen = False
          .BackgroundQuery = True
          .RefreshStyle = xlInsertDeleteCells
          .SavePassword = False
          .SaveData = True
          .AdjustColumnWidth = True
          .RefreshPeriod = 0
          .PreserveColumnInfo = True
          .ListObject.DisplayName = "Query1"
          .Refresh BackgroundQuery:=False
      End With
      Selection.ListObject.QueryTable.Refresh BackgroundQuery:=False
  
  End Sub
  </code></pre>