Query text file : CSV « File Path « VBA / Excel / Access / Word






Query text file

 

Public Sub QueryTextFile()
    Dim Recordset As ADODB.Recordset
    Dim ConnectionString As String
    ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & ThisWorkbook.Path & ";" & _
      "Extended Properties=Text;"
    
    Const SQL As String = "SELECT * FROM Sales.csv WHERE Type='Art';"
    
    Set Recordset = New ADODB.Recordset
    Call Recordset.Open(SQL, ConnectionString, CursorTypeEnum.adOpenForwardOnly, LockTypeEnum.adLockReadOnly, CommandTypeEnum.adCmdText)
    Call Sheet1.Range("A1").CopyFromRecordset(Recordset)
    Recordset.Close
    Set Recordset = Nothing
End Sub

 








Related examples in the same category

1.Saves the active worksheet in the workbook named MyWorkbook.xls as a comma-delimited text file named test.csv:
2.Split demonstration
3.Brings up a dialog box that asks the user for a filename.
4.Create text file based database
5.Export active worksheet to CSV file