Results 1 to 8 of 8

Thread: Detremine if text file open

  1. #1

    Thread Starter
    Addicted Member SkyCoder's Avatar
    Join Date
    Oct 2003
    Location
    Moon Crater 25
    Posts
    183

    Detremine if text file open

    Hi All

    How do I determine the status of a text file. Ie is the file open or not. (the text file is bieng used in a mail merge and i get an error when trying to overrite it)

  2. #2
    PowerPoster Keithuk's Avatar
    Join Date
    Jan 2004
    Location
    Staffordshire, England
    Posts
    2,236
    If your trying to open a file thats already open then you will get a error 55 - File already open. You must decide what you want to do. Close will shutdown all open files in your app or Close #1 (1 = file number allocated). You will have to sort your own file numbers.

    Make a function to open the specific file, if theres an error then Close it again so you can use it. It should return a Boolean, True if already open.

  3. #3
    Frenzied Member Buzby's Avatar
    Join Date
    Jan 1999
    Location
    UK
    Posts
    1,670
    You could just catch the error and process it - in other words, do

    On Error Go To FileInUse

    Open "thisfile" For Output as #1
    ..
    ..
    ..



    FileInUse:
    Msgbox "The file is in use - please try again later."
    'Buzby'
    Visual Basic Developer
    "I'm moving to Theory. Everything works there."

  4. #4

    Thread Starter
    Addicted Member SkyCoder's Avatar
    Join Date
    Oct 2003
    Location
    Moon Crater 25
    Posts
    183
    Thanks Buzby but

    the section "On Error Go To FileInUse" is highlited in red if I do that. Does anything have to be declared or what

  5. #5
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Go To should be GoTo.
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  6. #6

    Thread Starter
    Addicted Member SkyCoder's Avatar
    Join Date
    Oct 2003
    Location
    Moon Crater 25
    Posts
    183
    Can i use this "On error" in a function aswell

  7. #7

    Thread Starter
    Addicted Member SkyCoder's Avatar
    Join Date
    Oct 2003
    Location
    Moon Crater 25
    Posts
    183
    Public Function rs_letter(ByRef rsSource As ADODB.Recordset, WbrId As String) As Integer
    Dim lFld As Long
    Dim lRow As Long
    Dim LogFileNumber, x, x1, BS As Integer
    Dim LogFileName, sFile, sFields, sVals As String
    On Error GoTo Err

    If Not rsSource Is Nothing Then
    sFile = "LetterData"
    LogFileNumber = FreeFile ' Get unused file number.
    LogFileName = App.path & "\Correspondence\" & sFile & ".txt"
    ' On Error GoTo FileInUse
    Open LogFileName For Output As LogFileNumber ' Create file for append.

    rsSource.MoveFirst
    If rsSource.Status = adStateClosed Then
    sFields = "Current Date|"
    sVals = sVals & Format(Date, "YYYY/MM/DD") & "|"
    rsSource.MoveNext
    For x1 = 0 To 1
    For x = 1 To 10
    If x1 = 0 Then
    sFields = sFields & "Seller " & rsSource.Fields(x).Name & "|"
    Else
    sFields = sFields & "Buyer " & rsSource.Fields(x).Name & "|"
    End If
    If x1 = 0 Then
    sVals = sVals & rsSource.Fields(x).Value & "|"
    Else
    sVals = sVals & rsSource.Fields(x).Value & "|"
    End If
    Next x
    If x1 = 0 Then
    rsSource.MovePrevious
    End If
    Next x1

    For x = 11 To 35
    sFields = sFields & rsSource.Fields(x).Name & "|"
    If rsSource.Fields(x).Value <> "" Then
    sVals = sVals & CStr(rsSource.Fields(x).Value) & "|"
    Else
    sVals = sVals & "|"
    End If
    Next x
    Print #LogFileNumber, sFields
    Print #LogFileNumber, sVals
    End If
    End If
    Close #LogFileNumber
    Err:
    MsgBox "The file is in use - please try again later."
    End Function


    It doesnt seem to work in this function (It excecute even if there is no error)

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    You need to change the last few lines so that the message is only shown if there is actually an error. The label (Err: in this case) is just a marker, and doesn't have any effect on the code.

    Here are two solutions:
    VB Code:
    1. Close #LogFileNumber
    2.  
    3. Exit Function
    4. Err:
    5. MsgBox "The file is in use - please try again later."
    6. End Function

    VB Code:
    1. Close #LogFileNumber
    2. Err:
    3. If Err <> 0 Then
    4.   MsgBox "The file is in use - please try again later."
    5. End If
    6. End Function

Posting Permissions

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



Click Here to Expand Forum to Full Width