' see Chapter 14 for instructions

Sub CreateTextFile2()
  Dim conn As New ADODB.Connection
  Dim rst As ADODB.Recordset
  Dim strPath As String
  Dim strData As String
  Dim strHeader As String
  Dim strSQL As String
  Dim fso As Object
  Dim myFile As Object
  Dim fld As Variant

  Set fso = CreateObject("Scripting.FileSystemObject")
  Set myFile = fso.CreateTextFile( _
      "C:\VBAExcel2019_ByExample\ProductsOver20.txt", True)

  strPath = "C:\VBAExcel2019_ByExample\Northwind 2007.accdb"

  conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;" _
      & "Data Source=" & strPath & ";"
  conn.CursorLocation = adUseClient
  strSQL = "SELECT * FROM Products WHERE [List Price] > 20"

  Set rst = conn.Execute(CommandText:=strSQL, Options:=adCmdText)

  ' save the recordset as a tab-delimited file
  strData = rst.GetString(StringFormat:=adClipString, _
      ColumnDelimeter:=vbTab, RowDelimeter:=vbCr, _
      nullExpr:=vbNullString)

  For Each fld In rst.Fields
      strHeader = strHeader + fld.Name & vbTab
  Next
  With myFile
      .WriteLine strHeader
      .WriteLine strData
      .Close
  End With
End Sub

