+ Reply to Thread
Results 1 to 6 of 6

IsFileOpen function not working correctly

Hybrid View

rjtools IsFileOpen function not... 06-03-2008, 04:50 PM
fecurtis Post the code used in that... 06-03-2008, 04:57 PM
rjtools I took most all of the code... 06-04-2008, 10:04 AM
royUK rjtools You must read the... 06-04-2008, 10:19 AM
royUK I can't test this but try... 06-04-2008, 10:30 AM
rjtools Thanks, I tried it and it... 06-04-2008, 01:42 PM
  1. #1
    Registered User
    Join Date
    06-03-2008
    Posts
    3

    IsFileOpen function not working correctly

    I'm new to this form and hope you can help me. I found a "IsFileOpen" function and everything seemed to work perfectly. However, if serveral people try to use my code you will occationially get the message "File is now available" with the Read/Write and Cancel buttons. Is there a way to prevent this from happening. I don't want the many end users to get this message. I would just like them to get my message that says it's currently in use and try later. There are many IsFileOpen functions that I have found and they all function the same way. They work fine unless several users are using at the same time.

    Any help would be most apprecitated! Thanks.

  2. #2
    Forum Contributor
    Join Date
    04-30-2008
    Posts
    105
    Post the code used in that function and the sub procedures that call it (if applicable).

  3. #3
    Registered User
    Join Date
    06-03-2008
    Posts
    3
    I took most all of the code out so you wouldn't have to go through it. With that code removed it still happens. The first message will say "A file named ...xls aready exists in the in this locations. Do you want to replace it."
    Thanks for you help!
    Sub PickShift()
        Dim c As Variant
        Dim Dataloc As String
        
    CellAddr = ActiveCell.Address
    
    If ActiveCell.Offset(0, 8) = True Then
        ws = Range("L1").Value
        Dim UserID As String
        YourName = InputBox("Enter Your Name:   ", "Welcome ")
    
        Dim WbSource As Workbook, WbDest As Workbook
    
    
        Set WbSource = ThisWorkbook 'book where this code is located
        On Error Resume Next 'enables continued processing if an error occurs
        
            Dataloc = "P:\CUSTSERVICE\Custserv Online Sign Ups\rb\OSM RB BobThuli.xls"
    
           ' Display a message stating the file in use.
            If IsFileOpen(Dataloc) Then
            MsgBox ("File is currently in use by another user.  Please try again later." _
            & vbNewLine & "If this lasts over 15 minutes contact the I-team.")
            Exit Sub
            End If
        
        
            Workbooks.Open Dataloc, , , , "HotIssue", "HotIssue"
        
    '
    MsgBox ("Just Tesing")
         Application.ScreenUpdating = True 'Turn back on Screen updating
    
    End If
    
     ActiveWorkbook.Save
     ActiveWorkbook.Close
     Application.ScreenUpdating = True 'Turn back on Screen updating
     Success = MsgBox("You have successfully picked up this shift.", , "Success")
    End Sub
    
    ' This function checks to see if a file is open or not. If the file is
    ' already open, it returns True. If the file is not open, it returns
    ' False. Otherwise, a run-time error occurs because there is
    ' some other problem accessing the file.
    
    Function IsFileOpen(filename As String)
        Dim filenum As Integer, errnum As Integer
    
        On Error Resume Next   ' Turn error checking off.
        filenum = FreeFile()   ' Get a free file number.
        ' Attempt to open the file and lock it.
        Open filename For Input Lock Read As #filenum
        Close filenum          ' Close the file.
        errnum = Err           ' Save the error number that occurred.
        On Error GoTo 0        ' Turn error checking back on.
    
        ' Check to see which error occurred.
        Select Case errnum
    
            ' No error occurred.
            ' File is NOT already open by another user.
            Case 0
             IsFileOpen = False
    
            ' Error number for "Permission Denied."
            ' File is already opened by another user.
            Case 70
                IsFileOpen = True
    
            ' Another error occurred.
            Case Else
                Error errnum
        End Select
    
    End Function
    Last edited by royUK; 06-04-2008 at 10:19 AM.

  4. #4
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    rjtools

    You must read the Forum Rules and in future use Ciode tags when posting VBA to the Forum. I will add them this time.
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  5. #5
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    I can't test this but try changing the code by switching off alerts like this

        On Error Resume Next 'enables continued processing if an error occurs
      Application.DisplayAlerts = False
            'Dataloc = "P:\CUSTSERVICE\Custserv Online Sign Ups\rb\OSM RB BobThuli.xls"
           ' Display a message stating the file in use.
            If IsFileOpen(Dataloc) Then
            MsgBox ("File is currently in use by another user.  Please try again later." _
            & vbNewLine & "If this lasts over 15 minutes contact the I-team.")
            Exit Sub
            End If
        
        
            Workbooks.Open Dataloc, , , , "HotIssue", "HotIssue"
      Application.DisplayAlerts = True

  6. #6
    Registered User
    Join Date
    06-03-2008
    Posts
    3
    Thanks, I tried it and it seems much better but I did still get the message to come up but less frequently. If you have any other suggestions I would appreciate your help!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1