title: Using VBA to query a VIEW from SQL Server
body: <p>I am trying to create a VBA script that will pull the results from a View (<code>SELECT * FROM view_name</code>) from the <code>RecordSet.Source</code> property, but when attempted, my <em>CloseConnection</em> error handler keeps getting caught. I can get results from a table using a simple query like <code>SELECT * FROM tbl_name</code> with no issues. </p>
  
  <p>Below is the code I am using. Note: my Const variable has the Provider and Database information removed.</p>
  
  <p>I guess it really comes down to is it even possible to get results from a View like I would from a table?</p>
  
  <pre class="lang-vb prettyprint-override"><code>Option Explicit
  
  Const ConStrMSSQL As String = _
  "Provider=provider_name;Database=database_name;Trusted_Connection=yes;"
  
  Sub test()
  
      Dim formConnect As ADODB.connection
      Dim formData As ADODB.recordSet
      Dim formField As ADODB.Field
  
      Set formConnect = New ADODB.connection
      Set formData = New ADODB.recordSet
  
      formConnect.ConnectionString = ConStrMSSQL
  
      formConnect.Open
  
      On Error GoTo CloseConnection
  
      With formData
          .ActiveConnection = formConnect
          .Source = "SELECT * FROM v_data_extract_658"
          .LockType = adLockReadOnly
          .CursorType = adOpenForwardOnly
          .Open
      End With
  
      On Error GoTo CloseRecordset
  
  
      Sheets("test").Range("A1").Select
  
      For Each formField In formData.Fields
          ActiveCell.Value = formField.Name
          ActiveCell.Offset(0, 1).Select
      Next formField
  
      Sheets("test").Range("A2").CopyFromRecordset formData
  
      On Error GoTo 0
  
      CloseRecordset:
          formData.Close
  
      CloseConnection:
          formConnect.Close
  
  
  End Sub
  </code></pre>
  
  <p>This is the error message: </p>
  
  <blockquote>
    <p>run-time error 2147467259 (80004005): unknown token received from SQL Server </p>
  </blockquote>