title: VBA Access: Import CSV with additonal header data
body: <p>I am new to coding VBA. Was wondering if you all could help me? I have a CSV file which is structured as the following:
  - First 22 rows cover the specfic header data(this all loads in one column in excel)
  - column headers for table are in Row 23
  - the data is actually located from row 24 onward.</p>
  
  <p>What the code needs to do is insert this data in new table with the right column titles. Also while inserting it needs to input the file name and header data in the first few columns of the table.</p>
  
  <p>So far I have imported the entire CSV into an array I believe:</p>
  
  <p>See what I have so far:</p>
  
  <pre><code>Sub readCSV()
  Dim fs As Object
  Dim fso As New FileSystemObject
  Dim tsIn As Object
  Dim sFileIn, filename As String
  Dim aryFile, aryHeader, aryBody As Variant
  
  sFileIn = "C:\doc\test.csv"
  
  Set filename = fso.GetFileName(sFileIn)
  
  Set fs = CreateObject("Scripting.FileSystemObject")
  Set tsIn = fs.OpenTextFile(sFileIn, 1)
  sTmp = tsIn.ReadAll
  aryFile = Split(sTmp, vbCrLf)
  
  For i = 1 To 22
      aryHeader(1, i) = aryFile(i)
  Next i
  
  For i = 23 To UBound(aryFile)
      aryBody(i) = Split(aryFile(i), ",")
      DoCmd.RunSQL "INSERT INTO MAINS VALUES (filename,aryHeader(1),aryBody(i))"
  Next i
  
  End Sub
  </code></pre>
  
  <p>is this correct? Can anyone see of i am taking the right approach</p>
  
  <p>UPDATE - recoded this a bit</p>