reading from txt file into table

R

rachel

I'm trying to read data line by line from a delimited text file with over
1000 fields into a new table. I only want to bring in a handful of fields
from the beginning of the file (at this point). The actual number of fields i
bring in will vary from time to time, as will the total number of fields in
the file. But, i will always bring in all fields that don't start with "A".

I'm not going the normal "import text file with specifications" route
because the file changes over time, the names of the fields i want to import
change over time (except they'll never start with "A"), and since there are
over 1000 fields in the file I can't ever see all the fields in the
import/export wizard to determine which ones i want to import (not
necessarily a problem right now, since all the fields i'm currently focused
on are at the beginning of the file, but eventually i'll want to adapt this
to bring in other fields in the file).

I've gotten as far as reading in the first line from the file to create the
table where the data will be stored (because i won't know from the start how
many fields will be in the file, or how many i'll be importing, the table
isn't created beforehand). This is what i have so far:

strSourceFile = "C:\Data.txt"
lngInputFile = FreeFile
Open strSourceFile For Input As lngInputFile
iCnt = 1
While Not EOF(lngInputFile)
Line Input #lngInputFile, strInText
strArray = Split(strInText, "~")
Select Case iCnt
Case 1
x = 0
For i = 0 To UBound(strArray)
If Not strArray(i) Like "A*" Then
fld = strArray(i)
With tdf
.Fields.Append .CreateField(fld, dbText)
End With
x = x + 1
End If
Next i
db.TableDefs.Append tdf
numFields = x
End Select
iCnt = iCnt + 1
Wend
Close lngInputFile

I'm new at this, so i'm stuck on how to actually import the data into the
table. Do i continue reading the data in line-by-line... and if so, what do i
need to do? I've looked up help on recordsets, but i'm still at a loss. Is
there another way i could/should go about this?

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top