Solved

A Question About Read Text Files Into a Recordset

Posted on 2002-06-05
5
224 Views
Last Modified: 2011-10-03
I use ADO and the ODBC text driver to open a text file and read it into a recordset.

This is my code:

    Dim cnCSV As New ADODB.Connection
    Dim rs As New ADODB.Recordset

    cnCSV.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
               "Data Source=" & App.Path & "\;" & _
               "Extended Properties='text;HDR=NO;FMT=Delimited'"
    rs.Open "Select * From " & "Test.TXT", cnCSV, adOpenStatic, adLockReadOnly, adCmdText

But this code doesn't work with fields in the text file are delimited by space.

For example,

This is the conten of the text file

Jerry  M  28
Jonh  M  26
   ...

This is the debug info

debug.print rs.fields.count

the output is: 1

How to specify a space-delimited format?

Thanks a lot!
0
Comment
Question by:Jerryleo
5 Comments
 
LVL 43

Accepted Solution

by:
TimCottee earned 50 total points
ID: 7055867
Personally I wouldn't use the Jet Driver for this unless you really need to there is a MS Text driver. Also you need to create a Schema.ini file in the same folder as the text file with the appropriate definitions for the table:

[test.txt]
ColNameHeader=False
Format=Delimited( )
MaxScanRows=25
CharacterSet=OEM
Col1=F1 Char Width 255
Col2=F2 Char Width 255
Col3=F3 Char Width 255

For example.

If you use a different connection string such as:

"Provider=MSDASQL.1;Persist Security Info=False;Extended Properties=DBQ=" & app.Path & ";DefaultDir=" & App.Path & ";Driver={Microsoft Text Driver (*.txt; *.csv)};DriverId=27;Extensions=None,asc,csv,t1,tab,txt;FIL=text;MaxBufferSize=2048;MaxScanRows=25;PageTimeout=5;SafeTransactions=0;Threads=3;UID=admin;UserCommitSync=Yes;"

It should work, it may work with the Jet provider as well as this will also add the text driver in the background. However unless you need to distribute the Jet provider for other reasons it is a little bit overkill to use it just for reading text files.
0
 
LVL 3

Expert Comment

by:n_narayanan
ID: 7055994
Rather you can go for open command for text files.
This will work for tab and space delimited


OPEN "c:\myfile.dat" FOR INPUT AS #1
'Assume that you have 3 fields
DIM fld1, fld2, fld3
WHILE NOT EOF(1)
  INPUT #1, fld1, fld2,fld3
  debug.print fld1
  debug.print fld2
  debug.print fld3
WEND
CLOSE #1

Cheers
Narayanan

0
 
LVL 10

Expert Comment

by:arana
ID: 7057336
ADODataSet.DatabaseProvider := 'Microsoft.Jet.OLEDB.4.0';
ADODataSet.DatabaseConnect := 'Data Source=C:\TxtFiles;Extended Properties="Text;"';
ADODataSet.TableName := 'TxtFile#csv';

SCHEMA.INI file must be created and placed into same directory as TxtFile.csv (in this example C:\TxtFiles). Content of SCHEMA.INI:

[TxtFile.csv]
Format=Delimited(;)

if you use delimited() inside the parentesis should be your custom character, i have not tried it with a SPACE character, give it a shot.
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7057499
learning...
0
 

Author Comment

by:Jerryleo
ID: 7060864
Hello TimCottee,

Thank you very much!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question