Hi Nadine,
There are many possible approaches. By the sound of it you are able to
export a file that contains all the data you need and the only problem
is that all the lines are 120 characters long. The first three lines and
the last line are padded out to 120 characters with spaces, and you need
to truncate these lines to 80 characters.
If that's the case, the simplest thing is probably to export the file as
now, and then use VBA code to truncate the lines in question. I spent a
few minutes modifying a VBA function I'd written for a similar purpose,
and have pasted it at the end of this message.
Thanks for the reply.
I will try to explain further.
I have created the table in access and I transfer text out to notepad for
use with a banking application. The problem is that the first line of the
file should only be 80 characters long same for the second and third. Next
comes the detail lines which are 120 characters long , there can be more than
one detail line and finally the last line has to be 80 characters. So when I
created the table I brought the struture out the the longest width, but the
bank application crashes when it hits the spaces and on the first few lines.
Have you any thoughts as to what I could do.
Function TruncateFirstAndLastLines( _
ByVal FileSpec As String, _
Optional ByVal BackupExtension As String = "") As Long
'Truncates the first and last lines of a text file
'Optionally leaves the original file with its extension
'changed to BackupExtension.
'Returns 0 on success, otherwise the number of the error.
Dim fso As Object 'Scripting.FileSystemObject
Dim fIn As Object 'Scripting.TextStream
Dim fOut As Object 'Scripting.TextStream
Dim fFile As Object 'Scripting.File
Dim strFolder As String
Dim strNewFile As String
Dim strBakFile As String
Dim lngNumLines As Long
Dim j As Long
Const TRUNC_LEN = 80 'length to which to truncate
Const TRUNC_HEAD = 3 'number of lines to truncate at start of file
Const TRUNC_TAIL = 1
On Error GoTo Err_TruncateFirstAndLastLines
Set fso = CreateObject("Scripting.FileSystemObject")
With fso
'Handle relative path in Filespec
FileSpec = .GetAbsolutePathName(FileSpec)
strFolder = .GetParentFolderName(FileSpec)
strNewFile = .BuildPath(strFolder, fso.GetTempName)
'Scan file to count lines
Set fIn = .OpenTextFile(FileSpec, ForReading)
Do Until fIn.AtEndOfStream
fIn.SkipLine
Loop
lngNumLines = fIn.Line - 1
'Raise an error if there aren't enough lines in the file
If lngNumLines <= TRUNC_HEAD + TRUNC_TAIL Then
Err.Raise (-99)
End If
'Otherwise ...
fIn.Close
'Open files
Set fIn = .OpenTextFile(FileSpec, ForReading)
Set fOut = .CreateTextFile(strNewFile, True)
'Truncate header lines
Do Until fIn.Line = TRUNC_HEAD + 1
fOut.WriteLine Left(fIn.ReadLine, TRUNC_LEN)
Loop
'Read and write body of file
Do Until fIn.Line = lngNumLines - TRUNC_TAIL + 1
fOut.WriteLine fIn.ReadLine
Loop
'Truncate footer lines
Do Until fIn.AtEndOfStream
fOut.WriteLine Left(fIn.ReadLine, TRUNC_LEN)
Loop
fOut.Close
fIn.Close
'Rename or delete old file
If Len(BackupExtension) > 0 Then
strBakFile = .GetBaseName(FileSpec) _
& IIf(Left(BackupExtension, 1) <> ".", ".", "") _
& BackupExtension
If .FileExists(.BuildPath(strFolder, strBakFile)) Then
.DeleteFile .BuildPath(strFolder, strBakFile), True
End If
Set fFile = .GetFile(FileSpec)
fFile.Name = strBakFile
Set fFile = Nothing
Else
.DeleteFile FileSpec, True
End If
'Rename new file
Set fFile = .GetFile(strNewFile)
fFile.Name = .GetFileName(FileSpec)
Set fFile = Nothing
Set fso = Nothing
End With
'normal exit
TruncateFirstAndLastLines = 0
Exit Function
Err_TruncateFirstAndLastLines:
TruncateFirstAndLastLines = Err.Number
End Function