Source code for Issue Number 26

Copyright 1997-2000 by C&D Programming Corp. All Rights Reserved. Source code may not be reproduced except for use in a compiled executable. All rights reserved. If you would like to reprint any or all of this code please email us at info@codeoftheweek.com

Code of the Week Home


Source Code

Just paste this code into any module and change the name of the module to basTextFiles.

'----------------------------------------------------------------------
'
'   Module Name:    basTextFiles
'   Written By:     C&D Programming Corp.
'                   Martin Cordova
'   Create Date:    10/97
'   Copyright:      Copyright 1998 by C&D Programming Corp.  Source
'                   code may not be reproduced except for use in a
'                   compiled executable.  All rights reserved.  If
'                   you would like to reprint any or all of this
'                   code please email us at info@codeoftheweek.com
'----------------------------------------------------------------------
Public Function FixUpFilename(sFile As String) As String
    Dim sPath As String

    On Error Goto Handler
    '---file includes path?
    If InStr(sFile, "\") = 0 Then
        '---assume App.Path
        sPath = App.Path
        If Right(sPath, 1) <> "\" Then
            FixUpFilename = sPath & "\" & sFile
        Else
            FixUpFilename = sPath & sFile
        End If
    Else
        FixUpFilename = sFile
    End If
    Exit Function

Handler:
    Err.Raise Err.Number
End Function

'---read a text file with one line of code
'---file: file name with or without path
'---if path not included assumes App.Path
Public Function GetTextFile(ByVal sFile As String) As String
    Dim iFnum As Integer

    On Error GoTo ErrorHandler

    sFile = FixUpFilename(sFile)

    '---read file into buffer
    iFnum = FreeFile
    Open sFile For Input As #iFnum
    GetTextFile = Input(LOF(iFnum), iFnum)
    Close iFnum

    Exit Function

ErrorHandler:
    Err.Raise Err.Number
End Function


'---save a text file with one line of code
'---file: file name with or without path
'---if path not included assumes App.Path
Public Sub SaveTextFile(ByVal sFile As String, ByVal sBuffer As String)
    Dim sPath As String
    Dim iFnum As Integer

    On Error GoTo ErrorHandler

    sFile = FixUpFilename(sFile)

    iFnum = FreeFile
    Open sFile For Output As #iFnum
    Print #iFnum, sBuffer
    Close iFnum

    Exit Sub

ErrorHandler:
    Err.Raise Err.Number
End Sub