Solved

Read Text Files Into a Recordset - Semicolon Delimiter

Posted on 2001-06-18
8
5,041 Views
Last Modified: 2008-03-17
Hi experts,
I am very new in Visual Basic and still learning.
I try to use ADO and the ODBC text driver to open a text file an read it into a recordset.
Below is what I'm doing now in my program :

'=============================================
Dim connCSV As New ADODB.Connection
Dim rsTest As New ADODB.Recordset

connCSV.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" _
 & path & ";Extended Properties='text;HDR=NO;FMT=Delimited'"

rsTest.Open "Select * From test.txt", _
       connCSV, adOpenStatic, adLockReadOnly, adCmdText
Do While Not rsTest.EOF
MsgBox rsTest(0)  
rsTest.movenext
Loop
'==========================================

If I have a text file like this :
John, Los Angeles, USA
Mary, New York, USA
I will have rsTest(0) as John, and then Mary.
So far it works, but I must use comma delimiter in my text file.

My question is :
Is it possible to use other character as delimiter in my text file, like semicolon (;) ?
FYI :
- I use Oracle 8, VB 6, Win2K Pro
- At the first time I didn't have any Schema.ini file, then I tried to copy schema.ini file to c:\WinNT\System32\ and configured it by adding 1 line : "Format=Delimited(;)", but still can't work.
 
I look forward to your early reply :-)
Your help will be very much appreciated.

Rgds,
Kiky
0
Comment
Question by:kiky
8 Comments
 
LVL 1

Expert Comment

by:darthg8r
ID: 6204901
The default FMT is CSV try
connCSV.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" _
& path & ";Extended Properties='text;HDR=NO;FMT= "_
"Delimited(ascii)'"
where ascii is any character besides a double quote "
0
 

Author Comment

by:kiky
ID: 6204921
Hi dartq8r,
Thanks for your reply.
I tried this one :
connCSV.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" _
& path & ";Extended Properties='text;HDR=NO;FMT=Delimited(#59)'"

But still can't work.
The rsTest(0) still contain the first row data, not only the first field.

Rgds,
Kiky
0
 

Author Comment

by:kiky
ID: 6204928
I'm sorry this is the first time I ask question to expert-exchange.
This question is still open to anyone else, but I don't know how to change the status...






0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 5

Expert Comment

by:gbaren
ID: 6207548
You need to reject the answer. There should be an option at the bottom of the page.
0
 
LVL 7

Expert Comment

by:Z_Beeblebrox
ID: 6208451
Hi,

Try:

connCSV.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" _
& path & ";Extended Properties='text;HDR=NO;FMT=Delimited(;)'"
0
 

Author Comment

by:kiky
ID: 6209419
Hi dartq8r, Z_Beeblebrox, gbaren
Thanks for your reply.

I tried this one :
connCSV.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" _
& path & ";Extended Properties='text;HDR=NO;FMT=Delimited(#59)'"

connCSV.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" _
& path & ";Extended Properties='text;HDR=NO;FMT=Delimited(;)'"

Both of them can't work.
I think I need to reject the answer so I can change the status.
Anyway thank you very much for the help.

Rgds,
Kiky
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 260 total points
ID: 6211324
Try this:

Give this text file (d:\temp\test.txt):
John*Los Angeles*USA
Mary*New York*USA

And this Schema file (d:\temp\schema.ini):

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

The following code works for me:

Dim rs As ADODB.Recordset
Dim fld As Field

Set rs = New ADODB.Recordset
With rs
   .Source = "Select * From test.txt"
   .ActiveConnection = "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=d:\temp\;Extensions=asc,csv,tab,txt;"
   .CursorType = adOpenForwardOnly
   .LockType = adLockReadOnly
   .Open Options:=adCmdText
   Do While Not .EOF
      For Each fld In .Fields
         Debug.Print fld.Value,
      Next
      Debug.Print
      .MoveNext
   Loop
   .Close
End With
Set rs = Nothing
0
 

Author Comment

by:kiky
ID: 6213625
Dear acperkins,
It works ! :-)
Thank you very much for your help.

The point is yours :-)

Regards,
Kiky
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…

856 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