Here a most basic example to show how easy it is to do this with SQL on text
files.
The text file in this example is like this:
"Part_Number", "Part_Description"
1,"wheelnut"
2,"doormat"
3,"seatnut"
4,"lightnut"
So, this is the plain text file: C:\TextTables\Parts.txt
Sub TestLateBinding()
Dim rs As Object
Dim TextConn As String
Dim strSQL As String
TextConn = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & _
"C:\TextTables\;" & _
"Extended Properties=Text;"
strSQL = "SELECT PART_NUMBER FROM PARTS.TXT WHERE " & _
"PART_DESCRIPTION LIKE '%nut'"
Set rs = CreateObject("ADODB.Recordset")
rs.Open Source:=strSQL, _
ActiveConnection:=TextConn, _
CursorType:=0, _
LockType:=1, _
Options:=1
Cells(1).CopyFromRecordset rs
rs.Close
Set rs = Nothing
End Sub
If you use early binding so with a reference to ADO 2.x then the could can
be like this:
Sub TestEarlyBinding()
Dim rs As ADODB.Recordset
Dim TextConn As String
Dim strSQL As String
TextConn = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & _
"C:\TextTables\;" & _
"Extended Properties=Text;"
strSQL = "SELECT PART_NUMBER FROM PARTS.TXT WHERE " & _
"PART_DESCRIPTION LIKE '%nut'"
Set rs = New ADODB.Recordset
rs.Open Source:=strSQL, _
ActiveConnection:=TextConn, _
CursorType:=adOpenForwardOnly, _
LockType:=adLockReadOnly, _
Options:=adCmdText
Cells(1).CopyFromRecordset rs
rs.Close
Set rs = Nothing
End Sub
It has no error handling and you may need to add some more code, but this
will give you the idea.
RBS