Results 1 to 3 of 3

Thread: file already open in a network drive

  1. #1

    Thread Starter
    Frenzied Member wengang's Avatar
    Join Date
    Mar 2000
    Location
    Beijing, China
    Posts
    1,604
    one more question..

    If several computers are all using copies of an application that shares data held in text files on a common network drive (F , then is there a command that will prevent the program from editing the text file when it is already being edited by another PC file (to avoid the "FILE ALREADY OPEN" error)?


    I appreciate any helpful code or suggestions on this. THanks



  2. #2
    Lively Member
    Join Date
    Jun 2000
    Location
    Belgium
    Posts
    77
    Yes the command exist. It use and API call.
    Before Open the file, test it with the IsFileAlreadyOpen function
    Code:
    Private Declare Function lOpen Lib "kernel32" Alias "_lopen" (ByVal lpPathName As String, ByVal iReadWrite As Long) As Long
    Private Declare Function lClose Lib "kernel32" Alias "_lclose" (ByVal hFile As Long) As Long
    
    
    Function IsFileAlreadyOpen(FileName As String) As Boolean
     Dim hFile As Long
     Dim lastErr As Long
    
     ' Initialize file handle and error variable.
     hFile = -1
     lastErr = 0
    
     ' Open for for read and exclusive sharing.
     hFile = lOpen(FileName, &H10)
    
     ' If we couldn't open the file, get the last error.
     If hFile = -1 Then
        lastErr = Err.LastDllError
        Else
        ' Make sure we close the file on success.
        lClose (hFile)
     End If
    
     ' Check for sharing violation error.
     If (hFile = -1) And (lastErr = 32) Then
        IsFileAlreadyOpen = True
        Else
        IsFileAlreadyOpen = False
     End If
    End Function
    KWell

  3. #3
    Hyperactive Member
    Join Date
    Jul 2002
    Location
    Canada
    Posts
    455
    Hello wengang,

    I allways trying to get my source as simple as possible.
    When the next source is not working correctly, please tell me.

    Idee: when a file is used, it is not possible to rename it to his own name!


    Private Sub Form_Load()
    Dim SourceFile As String

    SourceFile = "R:\YourFile.XXX"

    On Error GoTo FileError
    Name SourceFile As SourceFile 'Rename file.
    GoTo FileEnd

    FileError:
    MsgBox "File Used by other application"

    FileEnd:
    On Error GoTo 0
    End Sub


    Nice regards,

    Michelle.

    [Edited by michelle on 07-14-2000 at 02:55 AM]

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