Solved

Read Text Files Into a Recordset - Semicolon Delimiter

Posted on 2001-06-18
8
4,927 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

863 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

25 Experts available now in Live!

Get 1:1 Help Now