Consulting

Results 1 to 2 of 2

Thread: Error if logfile is already open

  1. #1
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location

    Error if logfile is already open

    I use this code to test if I have a text file open. If it is already open it should either close it and reopen it or just associate it with #f. I have tested numerous ways and I cannot get it to work:

    [VBA]Function IsFileOK(stgFullPath As String, stgFileName As String) As Boolean

    Dim f As Integer
    IsFileOK = True

    On Error GoTo FileProblem
    f = FreeFile
    Open stgFullPath For Append As #f

    Exit Function

    FileProblem:
    Close stgFileName
    Open stgFullPath For Append As #f
    'IsFileOK = False

    End Function
    [/VBA]

    P.S. I use this textfile as a logfile to log what my users do with my program.
    Thank You,
    Daniel Blois
    http://studenthacker.blogspot.com/

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Function IsFileOK(stgFullPath As String, stgFileName As String) As Boolean

    Dim f As Integer
    IsFileOK = True

    On Error GoTo FileProblem
    f = FreeFile
    Open stgFullPath & stgFileName For Append As #f

    Exit Function

    FileProblem:
    f = f - 1
    Close #f
    Open stgFullPath & stgFileName For Append As #f
    'IsFileOK = False

    End Function
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •