About Ben Clothier

  • Website: http://www.AccessExperts.net or email
  • Biography: Ben Clothier is a Senior Access Developer at IT Impact, Inc., an Access MVP since 2009 and a SharePoint Technology Specialist. He has worked on business applications from a department's survey tracking to world-wide supply control database and in between. If he's not developing, he's probably reviewing or writing something about Access development! He recently co-authored Microsoft Access in SharePoint World. To learn more, visit AccessExperts.net for more details.

Posts by Ben Clothier:

0

Checking if files are locked using VBA

Posted by Ben Clothier on March 6, 2012 in Access Help, Outlook VBA, VBA |

In one of our client’s applications, we built an email template that sometimes may attach files to be sent out. Our client wanted a way to preview the attachment before they actually send the email. This created a dilemma – if they can preview and potentially edit the files, we certainly don’t want to continue and try to attach open files to an email, therefore creating a problem. So we needed a way to check whether files may be already open, alert the user to close the file, verify it’s closed then send the emails.

Get Exclusivity

Due to multitasking nature of Windows, there is no simple universal function to ask if a file, whatever type of file it may be, is open or not. However, we can at least approximate this functionality by attempting to open the file exclusively in our code and if we succeed, be reasonably sure that we are good to proceed onwards.

Introducing IsFileLocked() Function

When we open a text file with Notepad, Notepad does not place any locks on the file even when we dirty the file. In this situation, it does no harm to copy or read the file while it’s open by Notepad. Thus IsFileLocked() will return true for any .txt files opened by Notepad and in this situation, it’s generally OK. Of course, one shouldn’t try to write to the said file but that’s not what we are doing here. On the other hand, Word and Excel will place locks on their documents. Therefore trying to copy or read the file may be undesirable and threat it’s integrity. In this situation, our attempt to acquire an exclusive lock will fail, allowing us to alert the user to close the file themselves before proceeding further or cancel out.

The only significant caveat is that this procedure is not appropriate for checking whether a file is locked by other processes such as running background tasks. The locks can be acquired and released in milliseconds so calling the function is inherently racy. For purposes of checking whether users has a file open, this should be sufficient.

Public Function IsFileLocked(PathName As String) As Boolean
On Error GoTo ErrHandler
  Dim i As Integer

  If Len(Dir$(PathName)) Then
    i = FreeFile()
    Open PathName For Random Access Read Write Lock Read Write As #i
    Lock i 'Redundant but let's be 100% sure
    Unlock i
    Close i
  Else
    Err.Raise 53
  End If

ExitProc:
  On Error GoTo 0
  Exit Function

ErrHandler:
  Select Case Err.Number
    Case 70 'Unable to acquire exclusive lock
      IsFileOpen = True
    Case Else
      MsgBox "Error " & Err.Number & " (" & Err.Description & ")"
    End Select
  Resume ExitProc
  Resume
End Function

 

What if you have multiple files open?

That gets us to a good start but we also have to handle the fact that there may be more than one file open, and nobody likes being alerted by multiple dialogs. Thus we need to roll up the individual checks into a single message so the users can only see one message for all locked files that they may need to close. Here’s the code:

Public Function CheckForLockedFiles( _
  Files() As String _
) As Boolean
On Error GoTo ErrHandler
  Dim i As Long
  Dim lngLocks As Long
  Dim strFiles() As String
  Dim strMessage As String

  Do
    lngLocks = 0
    For i = 0 To UBound(Files)
      If IsFileOpen(Files(i)) Then
        ReDim Preserve strFiles(lngLocks)
        strFiles(lngLocks) = Files(i)
        lngLocks = lngLocks + 1
      End If
    Next
    If lngLocks Then
      strMessage = "The following files are in use. " & _
                   "Please close the application that may have it open." _
                   & vbNewLine & vbNewLine
      For i = 0 To UBound(strFiles)
        strMessage = strMessage & strFiles(i) & vbNewLine
      Next
      If vbCancel = MsgBox(strMessage, vbRetryCancel, "Files in use") Then
        CheckForLockedFiles = False
        Exit Do
      End If
    End If
  Loop Until lngLocks = 0
  If lngLocks = 0 Then
    CheckForLockedFiles = True
  End If

ExitProc:
  On Error GoTo 0
  Exit Function

ErrHandler:
  Select Case Err.Number
    Case 53 'File doesn't exist, ignore
      Resume Next
    Case Else
      MsgBox "Error " & Err.Number & " (" & Err.Description & ")"
  End Select
  Resume ExitProc
  Resume
End Function

Ben Clothier

Ben Clothier is a Senior Access Developer at IT Impact, Inc., an Access MVP since 2009 and a SharePoint Technology Specialist. He has worked on business applications from a department's survey tracking to world-wide supply control database and in between. If he's not developing, he's probably reviewing or writing something about Access development! He recently co-authored Microsoft Access in SharePoint World. To learn more, visit AccessExperts.net for more details.

More Posts - Website

2

Zip and Unzip from Microsoft Access VBA

Posted by Ben Clothier on February 6, 2012 in Access Help, VBA |

On occasions, we have a need to zip files as part of our workflow within Access VBA. One sore point with zipping is that there’s really no simple way to zip or unzip files without depending on a third-party utilities. When you think about it, it is quite odd considering that zipping is built-in to [...]

10

How to save your SharePoint password in Windows

Posted by Ben Clothier on January 18, 2012 in Access Help, Access Web Database, SharePoint |

A poster asked me a wonderful question: “How do I get Access [linked to SharePoint] to remember my login credentials so I don’t have to login everytime?” The answer to the question isn’t simple as it should be and hence, I felt an article on the subject would be helpful for those who want to [...]

2

New book for Access Web Database and SharePoint now available!

Posted by Ben Clothier on December 17, 2011 in Access Web Database, SharePoint, VBA |

As Juan may have mentioned earlier, I had been working with two other MVPs, Tim Runcie and George Hepworth in producing the first of its kind, a book dedicated to using Access with SharePoint. The book can be bought online at the Advisicon store. If you’re wanting to learn more about the capabilities and limitations of Access and SharePoint, [...]

0

How to work offline with SharePoint and Access 2010

Posted by Ben Clothier on December 8, 2011 in Access Help, Access Web Database, SharePoint, VBA |

One advantage a SharePoint list has over any ODBC data source is that Access can disconnect and re-connect and synchronize the local edits back to the server fairly seamlessly. It also provides a friendly conflict resolution management, all out of the box. This is available whether you’re using a traditional Access database linking a SharePoint list [...]

1

Running Access locally on a different domain using VPN

Posted by Ben Clothier on November 22, 2011 in Access Help |

We develop solutions using Access on the desktop and SQL Server on the network for clients through out the US. Our standard method is to login to the client’s network via VPN and program on their server, since we couldn’t program on our development machines using the credentials provided by our client. They would clash [...]

7

Query Pattern: Finding the maximum/minimum and getting the full row

Posted by Ben Clothier on November 17, 2011 in Access Help |

Authored by Ben Clothier The other day Juan needed my help with a query that required a special technique commonly known as the Strawberry query. He needed a way to match one group of records with the first occurrence of a item in another table. From that I decided to write this post to share the [...]

2

Online Utilities for Access Developers

Posted by Ben Clothier on September 29, 2011 in Access Help |

Frequently, we work remotely on our clients’ network, doing development work with one of their workstations and/or servers. As a consequence, it’s not always desirable or practical to install our favorite utilities. We therefore make good use of online utilities that can help make development easier when working remotely. 5 Great Online Tools Instant SQL Formatter – great for [...]

19

Access Services: Bringing your Access database to the web

Posted by Ben Clothier on September 19, 2011 in Access Help, Access Web Database, SharePoint |

Note: This is the third part of a three part series, you can find part one here and tpart two here. Authored by Ben Clothier To continue the series on Access and SharePoint integration (part 1 and part 2), we’ll be looking at the latest kid on the SharePoint’s block; Access Services. Access Services is included with SharePoint [...]

0

Stored Procedure Guide for Microsoft Access – Part 3

Posted by Ben Clothier on August 25, 2011 in Access Help |

Authored by Ben Clothier This is a continuation of series Juan started exploring using stored procedures with Access. To see the previous articles, visit part 1 and part 2.  If you have been following the series, Juan already has walked you through how to use a stored procedure on the Access side. We’ll now turn our attention [...]

Copyright © 1969-2012 Microsoft Access with SQL Server Blog All rights reserved.
This site is using the Desk Mess Mirrored theme, v2.0.2, from BuyNowShop.com.