ACC1x: Sample Function to Insert CR/LF in Fixed-Width Files

ID: Q109938


The information in this article applies to:


SUMMARY

Fixed-width text files imported into Microsoft Access must have a carriage return/line feed character at the end of each line. This article describes a sample Access Basic function to add carriage return/line feed characters to the end of each line in a file.

This article assumes that you are familiar with Access Basic and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information on Access Basic, please refer to the "Introduction to Programming" manual.


MORE INFORMATION

The following sample function will add a carriage return/line feed character to the end of each line in a file that does not have one. This function can process files with records larger than 255 bytes.

NOTE: In the following sample code, an underscore (_) is used as a line- continuation character. Remove the underscore when re-creating this code in Access Basic.


   Option Explicit

   Function AddCRLF (InFile As String, OutFile As String, _
      RecLen As Integer)

      Dim InFileNo As Integer, OutFileNo As Integer, Rec As String

      ' Get an open file handle and open the input file.
      InFileNo = FreeFile
      Open InFile For Input As #InFileNo

      ' Get an open file handle and open the output file.
      OutFileNo = FreeFile
      Open OutFile For Output As #OutFileNo

      ' Process the file.
      Do While Not EOF(InFileNo)
         ' Read one fixed-width record, RecLen in size.
         Rec = Input$(RecLen, InFileNo)
         ' Output the record to the output file.
         ' NOTE: Print automatically places a carriage return/line feed
         ' character at the end of the line.
         Print #OutFileNo, Rec
      Loop

      ' Close the files.
      Close #InFileNo
      Close #OutFileNo

   End Function 

To add carriage return/line feed characters to a file in the Microsoft Access directory called X300.TXT that has 30-byte records, type the following in the Immediate window:

? AddCRLF("C:\ACCESS\X300.TXT", "C:\ACCESS\X300CRLF.TXT", 30)

Note that the output file will be called X300CRLF.TXT.

Additional query words: Import Fixed Width FreeFile


Keywords          : kb3rdparty IsmTxtfx 
Version           : 1.0 1.1
Platform          : WINDOWS 
Issue type        : kbinfo 

Last Reviewed: April 2, 1999