Solved

A Question About Read Text Files Into a Recordset

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

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

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…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…

685 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