Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

A Question About Read Text Files Into a Recordset

Posted on 2002-06-05
5
Medium Priority
?
241 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 200 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying 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

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
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…
Suggested Courses

963 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