title: How to fix 'Fields aren't populating when creating recordset from workbook with two header rows'
body: <p>I am trying to adjust a vba excel macro that works to collect the data from all workbooks within a specified date range. I'm having difficulty being able to pull in workbooks that have two header rows of strings. When I try to run the macro on these workbooks, the Fields within the Recordset do not populate with the correct information.</p>
  
  <p>The code has previously worked for workbooks with only one header row of strings (with all other data below it being numerical data), but I'm now running into difficulty with workbooks that have two header rows of strings. If I delete one of the 1st 2 rows of strings in the workbook and try running, the macro works fine and the fields populate correctly.. However, this is not a feasible long term solution.</p>
  
  <pre class="lang-vb prettyprint-override"><code>Dim Header As Boolean
  Dim rsCon As Object
  Dim rsData As Object
  Dim szSQL As String
  Dim szConnect As String
  Dim sDFolder As String
  Dim SourceFile As String
  
  Header = True
  
  'Folder that contains several workbooks
  sDFolder = "C:\Users\kevin\Desktop\Test"
  
  'Example File from sDFolder
  SourceFile = "Data 2019-02-25.csv"
  
  ' Create the connection string
  szConnect = "Driver={Microsoft Text Driver (*.txt; *.csv)};" &amp; _
              "Dbq=" &amp; sDFolder &amp; ";" &amp; _
              "Extensions=asc,csv,tab,txt;"
  
  ' workbook level name
  szSQL = "SELECT * FROM " &amp; """" &amp; SourceFile &amp; """"
  
  Set rsCon = CreateObject("ADODB.Connection")
  Set rsData = CreateObject("ADODB.Recordset")
  
  rsCon.Open szConnect
  rsData.Open szSQL, rsCon, 0, 1, 1   'adOpenForwardOnly, adLockReadOnly, adCmdText
  
  If Not rsData.EOF Then
      If Header = True Then
          TargetRange.Cells(1, 1).CopyFromRecordset rsData
      End If    
  End If
  
  rsData.Close
  Set rsData = Nothing
  rsCon.Close
  Set rsCon = Nothing
  SetAttr sDFolder, vbNormal
  </code></pre>
  
  <p>The code above works for workbooks with one header, but the Fields in rsData don't populate correctly for workbooks with two headers.</p>