logo
vbRad Home
Source Code
Book Reviews
Forum
Links
About Us
Contribute

Compare Databases with SQL Effects Clarity
 
 Read any delimited file into a recordset

Posted on
1/18/2002
Author:
Robert Gelb
Email:
Not Shown
Applies To OS:
NT, 9x, 2000
Product:
5, 6



Download... (6 kb)

One of the most common questions that newbie and some not so newbie programmers ask is how to read in a text delimited file, such as a comma separated file. Most of these programmers do the following:

Private Sub LoadFile()
    Dim handle As Integer
    Dim sCurrentLine As String
    Dim arrayTokens() As String
    
    handle = FreeFile
    Open "c:\file.txt" For Input As handle
    Do While Not EOF(handle)
        Line Input #handle, sCurrentLine
        arrayTokens = Split(sCurrentLine, vbTab)
        'process arrayTokens
    Loop
End Sub

For those who don't understand the above code: we open the file, read it line by line, then split each line into tokens and then process those tokens. This method is too slow, too prone to error and makes it too inconvinient to deal with data.

The solution is to open the text file into a Recordset. Once the file is in the recordset, you can anything: Filter various attributes in and out, search for various fields of data, enumerate fields, XML (used here as a verb) the data, whatever. This is, of course, not as simple as opening a recordset from an Access table, but definetely worth the trouble.

So, as a result, here I am providing a reusable class that can open any kind of text delimited or separated file, be it comma separated or tab delimited or whatever else. It can use the the first line for column names, if you have those, or not. The code is as simple as this:

Private Sub LoadFile()
    Dim oRs As ADODB.Recordset
    Dim oTextHandler As clsTextHandler
    
    Set oTextHandler = New clsTextHandler
    With oTextHandler
        .Delimiter = vbTab
        .UseFirstLineForColumnNames = True
        .FileName = "c:\file.txt"
        If .Execute(oRs) Then
            MsgBox "Recordset Loaded"
        Else
            MsgBox "Failed" & vbCrLf & vbCrLf & .LastError
        End If
    End With
	
    Set oTextHandler = Nothing
    Set oRs = Nothing
End Sub
Pretty simple, ha? Included in the download is a demo program that demonstrates the functionality, as well as a sample tab delimited file that I used to test the class. To start the demo, download, start Project1.vbp, then press the Run button. Enjoy.

Download... (6 kb)




Add Your Comment  

Name: Email Address: all fields optional
Notify me via email when someone responds to this message (valid email required).

Enter the word:
 



Comments
#1. By harrell geron. Posted on 8/13/2006 10:18:04 AM
Can this work in an MS Access(2003) Module ie. Vbasic?

I have been trying to "walk XML files" to get the data to place in Access Tables.

The dang DOM is driving me crazy, I can't get much to work right.
I need to read xml files produced to the www.landxml.org specs.

This sounds like something that would work.

#2. By Author. Posted on 8/14/2006 4:53:59 PM
Sorry, I don't think this will work. There is really no substitute for learning the XML DOM. It really isn't all that complicated. And it looks like landxml has a prebuilt schema. You should be able to read it in easy. I would actually suggest doing this in .NET since the Dataset object is very adept in dealing with XML (particularly with a schema).

#3. By Versluis. Posted on 9/20/2006 2:28:48 PM
What if the text file has more than 255 columns in it. This method doesn't work then.Are there alternatives except splitting up the file into two files? Thanx in advance.

#4. By Author. Posted on 9/20/2006 4:53:51 PM
Answer to #3. Recordset supports more than 255 columns. Flexgrid might not, so your alternative is to get a better grid.