title: How to get SQL Query Error Details when QueryTable.Refresh fails
body: <p>Say I have this simple bit of VBA that refreshes a query table:</p>
  
  
  
  <pre class="lang-vb prettyprint-override"><code>With aListObject.QueryTable
      .refresh BackgroundQuery:=False
  End With
  </code></pre>
  
  <p>If the above table returns an error from the SQL engine upon refreshing, then how do I obtain the actual SQL error details?</p>
  
  <p>E.g. I want this type of message, which is returned in SQL Server Management Studio:</p>
  
  <blockquote>
    <p>Msg 8134, Level 16, State 1, Line 1 <br>Divide by zero error encountered.</p>
  </blockquote>
  
  <p>I can add error handling to the VBA to return the VBA error as such:</p>
  
  <pre class="lang-vb prettyprint-override"><code>On Error Resume Next    
  With aListObject.QueryTable
      .refresh BackgroundQuery:=False
      if err.number &gt; 0 then msgbox err.Description
  End With
  On Error GoTo 0
  </code></pre>
  
  <p>But this only returns this message:</p>
  
  <blockquote>
    <p>SQL Syntax Error</p>
  </blockquote>
  
  <p>How do I return the full SQL error message details? </p>
  
  <p>I have looked at the following but can't find anything that jumps out there:</p>
  
  <ul>
  <li><code>QueryTable</code> object (<a href="https://docs.microsoft.com/en-us/office/vba/api/excel.querytable" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/office/vba/api/excel.querytable</a>) </li>
  <li><code>QueryTable.Refresh</code> method (<a href="https://docs.microsoft.com/en-us/office/vba/api/excel.querytable.refresh" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/office/vba/api/excel.querytable.refresh</a>)</li>
  </ul>