+ Reply to Thread
Results 1 to 5 of 5

Really weird issue with xls being overwritten.

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    06-16-2011
    Location
    London
    MS-Off Ver
    Excel XP
    Posts
    276

    Really weird issue with xls being overwritten.

    My app contains 2 .xls workbooks, the main workbook and the database workbook. Users run the main workbook and do.....stuff, which writes data results to the database workbook.

    To guard against errors I have the following code in place in case two users try to submit data to the database workbook at the same time:

    Function IsFileLocked(filePath As String) As Boolean
         
        On Error Resume Next
        Open filePath For Binary Access Read Write Lock Read Write As #1
        Close #1
        If Err.Number <> 0 Then
            IsFileLocked = True
            Err.Clear
        Else
            IsFileLocked = False
        End If
        On Error GoTo 0
     
    End Function
    The above is called from the macro that tries to write data to the database workbook thusly:

    
    Dim path as string
    Path = Range("D14").Value
    
    Do Until IsFileLocked(Path) = False
    Loop
    
    Workbooks.Open (Path), Password:="temppassword"
    Sheets("db").Select
    Cells(user, 36).Value = testresult
    ActiveWorkbook.Save
    ActiveWindow.Close
    This works absolutely fine in a test environment, the main workbook will just pause for a few milliseconds when clicking 'Submit' until the database workbook is available. If I open the database workbook manually on another PC then the above code will just sit at an hourglass until I close it on said other PC. No problem.

    However, when testing in a larger, slower, environment I've found something very odd.
    Very, very rarely (so rarely in fact that it's impossible to replicate), instead of pausing until the database workbook is free instead it gets overwritten by a new, blank workbook with one single test result in cell A1.

    And I can't work out why. It might be something to do with a slow network or old, dodgy file server that's causing it to not be able to find the workbook file......

    Further testing has discovered that if the database workbook doesn't exist then the main workbook will create a new (blank) one and save it in the original database workbook's location. That I don't get either. Surely, the above code would error in the isfilelocked function and therefore return an = true value and loop?

  2. #2
    Valued Forum Contributor tlafferty's Avatar
    Join Date
    04-08-2011
    Location
    United States, Tacoma, WA
    MS-Off Ver
    Excel 2010, Excel 2013 Customer Preview
    Posts
    1,112

    Re: Really weird issue with xls being overwritten.

    hmmmm....not sure why either. You could try adding a line to send a ping packet to the file server and if response times are above say 70 ms, then generate the error.
    If your question has been satisfactorily addressed, please consider marking it solved. Click the Thread Tools dropdown and select Mark thread as solved.
    Also, you might want to add to the user's reputation by clicking the star icon in the lower left corner of the post with the answer- it's why we do what we do...

    Thomas Lafferty
    Analyst/Programmer

  3. #3
    Forum Contributor
    Join Date
    06-16-2011
    Location
    London
    MS-Off Ver
    Excel XP
    Posts
    276

    Re: Really weird issue with xls being overwritten.

    Well, what I've done to try and solve this is add the following to the isfilelocked function (original code in italics):

    
    
    Dim intReturn As Integer
    On Error GoTo FileMissing
    intReturn = GetAttr(filePath)
    
     On Error Resume Next
        Open filePath For Binary Access Read Write Lock Read Write As #1
        Close #1
        If Err.Number <> 0 Then
            IsFileLocked = True
            Err.Clear
        Else
            IsFileLocked = False
        End If
        On Error GoTo 0
        
        Exit Function
    
    
    
    FileMissing:
        
        IsFileLocked = True

    So the first thing the function does is try a GETATTR on the database workbook, if that errors then bypass the rest of the function and return an = true value.....which then causes the calling macro to loop until it does find the file. This tests ok. But I still dont get why it was overwriting the database workbook and I have a feeling it could be important so any help shedding light on the matter would be greatly appreciated.
    Last edited by swoop99; 09-15-2011 at 04:17 PM.

  4. #4
    Valued Forum Contributor tlafferty's Avatar
    Join Date
    04-08-2011
    Location
    United States, Tacoma, WA
    MS-Off Ver
    Excel 2010, Excel 2013 Customer Preview
    Posts
    1,112

    Re: Really weird issue with xls being overwritten.

    Not a clue. Ideas guys?

  5. #5
    Forum Contributor
    Join Date
    06-16-2011
    Location
    London
    MS-Off Ver
    Excel XP
    Posts
    276

    Re: Really weird issue with xls being overwritten.

    shameless <PUNT>

+ 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