Full Version: Question about adding a record to beginning of file
UtterAccess Discussion Forums > Microsoft® Access > Access Q and A
hammerva
Hello all.

I was wondering if it is possible to append a string to the beginning of a flat file. I have a process where I am creating a flat file based on the number of records. The header record (which is what I need to write) has a field called Number of Records which is basically the number of lines in the file. The problem is that this file is 3 tiered with a W10 record that can have multiple W20 records that may or may not have W30 records. So I won't really know how many records our on the file until the end.

Thanks ohyeah.gif
khaos
I have a chunk of code, used to delete a line from a file, that could be modified to suit your needs. I pulled it from a db called Neatcode, don't remember the source. Here is the code.

CODE

Sub FDeleteLine(ByVal PathName As String, ByVal LineToDelete As String)
'
' Deletes a line (LineToDelete) from a text file.
' This must be an exact match.  If not found, doesn't return an error.
'
Dim f As Integer, F2 As Integer, InLine As String, FName2 As String
Dim Drive As String, Path As String, FileName As String, Ext As String
  FParsePath PathName, Drive, Path, FileName, Ext
  FName2 = Drive & Path & Format(Time, "hhnnss") & ".TMP"
  f = FreeFile
  Open PathName For Input As #f
  F2 = FreeFile
  Open FName2 For Output As #F2
  Do While Not EOF(f)
    Line Input #f, InLine
    If InLine <> LineToDelete Then
      Print #F2, InLine
    End If
  Loop
  Close #f
  Close #F2
'
' Replace old file with new file
'
  Kill PathName
  Name FName2 As PathName
End Sub


HTH
Ken
cfletch
There's really two way to do it: either do all your pre-processing first, and then print the entire file, or print the file with a dummy line, which you'll then replace with the real value.

You could probably do the calculations for each record, and instead of printing it out to the file, just save it as an array. When you're done calculating, the number of items in the array is the number of records, then you can just print everything out at that point.

If you're not too familiar with arrays, you might just want to try the double-print way. Have a counter variable while you're writing it out normally, so you know how many records you'll have at the end, and just put a "Record count: XXX" where you should have the record count in the file. Then, you can adapt khaos' code to re-read the file, changing only that record count line.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.