Changing format of text file

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am trying to open two text files and write from one to the other changing
the format as I do it. The file I open to pull from has one long string of
data of indeterminate length. It can vary with each file. I want to open the
first file, pull the first 80 characters and put them on the first line of
the second file. Then grab the next 80 characters of the first file and put
them on the second line of second file. And so on until I am at the end of
the record of the first file. Or maybe its possible to just open the first
file and place a carrage return every charaters?

Any thoughts and/or help is greatly appreciated.
 
Gibson said:
I am trying to open two text files and write from one to the other changing
the format as I do it. The file I open to pull from has one long string of
data of indeterminate length. It can vary with each file. I want to open the
first file, pull the first 80 characters and put them on the first line of
the second file. Then grab the next 80 characters of the first file and put
them on the second line of second file. And so on until I am at the end of
the record of the first file. Or maybe its possible to just open the first
file and place a carrage return every charaters?


This is fairly easy using the File I/O statements in VBA.

See Open, Get, Put, Close in VBA Help - Contents - VB
language - Statements.
 
See Roger Carlson’s post (1/20/2006) in Importing and Exporting Data to Gary
G’s post Importing a large amount of data as 1 (1/19/2006)

This subroutine will read a text file that is a single string into a table
breaking it at 80 characters for each record.
'---------------------------
Sub ReadLongTextFile()
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim MyRecord As String
Dim i As Integer

Set dbs = CurrentDb
' Open sample file for binary access.
Open "c:\GetTest.txt" For Binary As #1
MyRecord = String(80, " ")
Set rst = dbs.OpenRecordset("CharacterTable")
Do While Not EOF(1)
Get #1, , MyRecord ' Read 80 char.
rst.AddNew
rst!Char80 = MyRecord
rst.Update
Loop
Close #1 ' Close file.
End Sub
'---------------------------

This assumes your file is "c:\GetTest.txt", your table is called
"CharacterTable", and your field in the table is called "Char80". Change
these values to match your database. It also requires a Reference to DAO to
be set.
 
Back
Top