• Checking for Open Files (Access 2000)

    Author
    Topic
    #428015

    Hi all,

    I have one form in an Access ADP project that shows a list of files in a remote server.
    On double click on a list item (file), the form will copy the file to a temporary directory in the local computer (say, “C:project_temp”) and then open the file using the shell32.dll library.

    As the project is to be run in several machines, for this to work, when the form opens it checks whether “C:project_temp” exists. If so, it deletes all files in it (so as not to waste disk space). If not, it creates the directory.

    The problem is, when someone opens the form, then double clicks to open (ie, copy to the local machine and open) a file, then closes the form and opens it again WITHOUT closing the open file first, I get Error 70 “access denied” (Access is trying to delete a file in use).

    What I’d like to do is, when someone opens the form, have Access check whether there’s an open file in “C:project_temp”, and if so, prompt the user accordingly. Any ideas how I can do this (specifically the “are there any open files?” part)?

    Thank you in advance

    Viewing 0 reply threads
    Author
    Replies
    • #993243

      Put this code in a module:

      Public Function IsFileOpen(FileName As String) As Boolean
      Dim f As Integer
      f = FreeFile
      On Error Resume Next
      Open FileName For Binary Access Read Write Lock Read Write As #f
      Close #f
      IsFileOpen = Not (Err.Number = 0)
      End Function

      Public Function AnyFileOpen(ByVal FolderName As String) As Boolean
      Dim FileName As String
      If Not Right(FolderName, 1) = “” Then
      FolderName = FolderName & “”
      End If
      FileName = Dir(FolderName & “*.*”)
      Do While Not FileName = “”
      If IsFileOpen(FolderName & FileName) Then
      AnyFileOpen = True
      Exit Do
      End If
      FileName = Dir
      Loop
      End Function

      Use like this:

      If AnyFileOpen(“C:Project_temp”) = True Then
      MsgBox “There is an open file.”, vbInformation
      Else

      End If

      • #993250

        Hello Hans,

        I can’t believe a) the VERY short time to reply and the multiplicity of choices!! The AnyFileOpen function alone would have done perfectly! (in fact, it did wink).

        Thanks exclamation

        • #993251

          The AnyFileOpen function needs the IsFileOpen function!

          • #993258

            You’re right. I’m working with a workmate and after overlooking the code I passed it over to him, who implemented it. I then checked his modified code and rechecked yours, but not just carefully enough. But I didn’t lie, your code DID work!!
            Sorry for the mistake sorry stupidme doh

    Viewing 0 reply threads
    Reply To: Checking for Open Files (Access 2000)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: