Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Modules / DAO / VBA / September 2007

Tip: Looking for answers? Try searching our database.

Parse text file

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Daniel - 26 Sep 2007 19:08 GMT
Hello,

I have the following code to read through a text file

   Dim FileNumber As Integer
   Dim sFile As String
   
   FileNumber = FreeFile
   Open strFile For Input As #FileNumber   'open the file
   
   Dim strLine As String
   Do Until EOF(FileNumber)                'loop through each line in the
file
       Line Input #FileNumber, strLine     'store the line as a variable
       ReadTxt = ReadTxt & strLine
   Loop
   Close #FileNumber                       'close the file

Then I do the following

   arrstrTxt = Split(ReadTxt, vbKeyReturn)
   
   For i = LBound(arrstrTxt) To UBound(arrstrTxt)
       Debug.Print "Line: " & i & vbCrLf & arrstrTxt(i)
   Next i

The problem being that it only returns 21 lines when in fact, when
viewed/printed it actually has 157.  I have tried variation in the split
function (chr(10), chr(13), vbcrlf,...) to no avail.

I analyzed the file a realized that each line has 132 characters.  Is there
a way to read through a file 132 characters at a time?  or any mistakes with
my prior code that would explain my problem?

Thank you for your help,

Daniel P
pietlinden@hotmail.com - 26 Sep 2007 19:30 GMT
> I analyzed the file a realized that each line has 132 characters.  Is there
> a way to read through a file 132 characters at a time?  or any mistakes with
[quoted text clipped - 3 lines]
>
> Daniel P

Use the Mid$ function to break the file into 132-character chunks.
Marshall Barton - 26 Sep 2007 20:27 GMT
>I have the following code to read through a text file
>
[quoted text clipped - 27 lines]
>a way to read through a file 132 characters at a time?  or any mistakes with
>my prior code that would explain my problem?

You should explore using the Get statement instead of Line
Input.

Signature

Marsh
MVP [MS Access]

Stuart McCall - 26 Sep 2007 20:47 GMT
> Hello,
>
[quoted text clipped - 35 lines]
>
> Daniel P

First of all you can ditch all that time-wasting Line Input stuff and grab
the whole file in one go:

Dim FileNumber As Integer
Dim sFile As String

FileNumber = FreeFile
Open strFile For Binary Access Read As FileNumber
sFile = Space(LOF(FileNumber))
Get #FileNumber, , sFile
Close FileNumber

Now you have the entire contents of the file in sFile, which means you can
process it all in RAM (much faster). Not only that but the embedded
end-of-line characters are still there, whereas Line Input strips them out.

Now if the file was created under Windows, the EOL chars will be vbCrLf.
However if it was created under UNIX, they will be vbCr (no linefeed). Lets
assume its a Windows file. Now you can use the Split function to get each
"line" into an array:

arrstrTxt = Split(sFile, vbCrLf)

I can see you already know how to loop over the array, so I'll leave it
there. Let me (us) know if you need further help.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.