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

Last reviewed: April 2, 1997
Article ID: Q109938
The information in this article applies to:
  • Microsoft Access versions 1.0, 1.1

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 : IsmTxtfx kb3rdparty
Version : 1.0 1.1
Platform : WINDOWS
Hardware : X86
Issue type : kbinfo


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: April 2, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.