Solved

A Question About Read Text Files Into a Recordset

Posted on 2002-06-05
5
222 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

813 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now