title: How can I fix the SQL Query error 'invalid column name'?
body: <p>I am running the following query in VBA on a SQL server. In order to simplify I use an alias for the different columns in the select clause. VBA returns the following error: </p>
  
  <blockquote>
    <p>Invalid column name 'QtyDCo'. </p>
  </blockquote>
  
  <p>I tried adding brackets and quotes, but it did not help. Does anyone have an idea on how to fix the error?</p>
  
  <pre><code>"SELECT Qry_ETD_Pos_Combined.colDate AS [Date], " &amp; _
      " Qry_ETD_Pos_Combined.colClrBroker AS [Clr Broker], " &amp; _
      " Qry_ETD_Pos_Combined.colAccount AS Account, " &amp; _
      " tblDefProd.colInstr AS [FT/OP], tblDefProd.colCcy, " &amp; _
      " Qry_ETD_Pos_Combined.colId AS Instrument,  tblDefDeriv.colExpiry, " &amp; _
      " tblDefProd.colMultiplier AS Multiplier, " &amp; _
      " IsNull(tblDatPositionsCalc.colLots,0) AS QtyDCo, " &amp; _
      " IsNull(tblDatPositions.colLots,0) AS QtyBrk, " &amp; _
      " QtyDCo - QtyBrk AS [Qty Diff], " &amp; _
      " IsNull(tblDatPositions.colMktPrice,0) AS [Price(ETD)], " &amp; _
      " IsNull(tblDatDeriv.colPrevClose,0) AS [Price(BBG)], " &amp; _
      " [Price(ETD)]-[Price(BBG)] AS [Mkt Price Diff], " &amp; _
      " Round(IsNull(tblDatPositionsCalc.colPrice,0),8) AS [Avg Price D&amp;Co], " &amp; _
      " IsNull(tblDatPositions.colPrice,0) AS [Avg Price Brk], " &amp; _
      " Round([Avg Price D&amp;Co]-[Avg Price Brk],5) AS [Avg Price Diff], " &amp; _
      " IIf([Qty D&amp;Co]=0,0,Round(IIf([FT/OP]='FT',tblDatPositionsCalc.colLots*tblDefProd.colMultiplier*([Price(ETD)]-[Avg Price D&amp;Co]),IIf([FT/OP]='OP',tblDatPositionsCalc.colLots*tblDefProd.colMultiplier*IsNull([Price(ETD)],[Price(BBG)]),0)),2)) AS [Mkt Val D&amp;Co], IsNull(tblDatPositions.colVarMar,0) AS [Mkt Val Brk], Round([Mkt Val D&amp;Co]-[Mkt Val Brk],2) AS [Mkt Val Diff], " &amp; _
      "Round(IIf([FT/OP]='OP',-[Qty D&amp;Co]*[Multiplier]*[Avg Price D&amp;Co],0),2) AS [Trade Val D&amp;Co], Round(IIf([FT/OP]='OP',-[Qty Brk]*[Multiplier]*[Avg Price Brk],0),2) AS [Trade Val Brk], [Trade Val D&amp;Co]-[Trade Val Brk] AS [Trade Val Diff], tblDatAdmFX.colSpot AS FX, " &amp; _
      "Round([Mkt Val D&amp;Co]*[FX],2) AS [Mkt Val CHF], Round([Trade Val D&amp;Co]*[FX],2) AS [Trade Val CHF] " &amp; _
      "FROM (((((" &amp; MyQryPosComb &amp; " AS Qry_ETD_Pos_Combined LEFT JOIN dbOps.dbo.tblDatPositions ON (Qry_ETD_Pos_Combined.colId = tblDatPositions.colId) " &amp; _
      "AND (Qry_ETD_Pos_Combined.colAccount = tblDatPositions.colAccount) " &amp; _
      "AND (Qry_ETD_Pos_Combined.colDate = tblDatPositions.colDate)) LEFT JOIN dbOps.dbo.tblDatPositionsCalc ON (Qry_ETD_Pos_Combined.colId = tblDatPositionsCalc.colId) " &amp; _
      "AND (Qry_ETD_Pos_Combined.colAccount = tblDatPositionsCalc.colAccount) " &amp; _
      "AND (Qry_ETD_Pos_Combined.colDate = tblDatPositionsCalc.colDate)) LEFT JOIN dbProd.dbo.tblDefDeriv ON Qry_ETD_Pos_Combined.colId = tblDefDeriv.colId) LEFT JOIN dbProd.dbo.tblDefProd ON tblDefDeriv.colProduct = tblDefProd.colProduct) LEFT JOIN dbMktData.dbo.tblDatDeriv ON (Qry_ETD_Pos_Combined.colDate = tblDatDeriv.colDate) " &amp; _
      "AND (Qry_ETD_Pos_Combined.colId = tblDatDeriv.colId)) LEFT JOIN dbMktData.dbo.tblDatFX ON Qry_ETD_Pos_Combined.colDate = tblDatFX.colDate " &amp; _
      "WHERE (tblDatFX.colCcy1=[tblDefProd].[colCcy] AND tblDatFX.colCcy2='CHF');"
  </code></pre>