Checking A Database File

This prompted me to do a little digging and I found out that it couldn’t access the file to check because it was open in MS Access 2. As a result I rewrote my routine to take into account file sharing, but it struck me that I could use the old routine to check if a database file was in use.

Some routines that check for the existence of a file might not work with a Microsoft Access 2.0 database that is in use. To check for the file, you must open it as shared. This could be used to check a database file before it’s compacted or repaired.

Function FileExists(Filename As String) As Integer
     On Error Resume Next
     Open Filename For Input Access Read Shared As #1
     FileExists = (Err = 0)
     Close #1
End Function

Also, you can use the following code to check to see if a database file is in use.

Function DBFileInUse(Filename As String) As Integer
     On Error Resume Next
     Open Filename For Input As #1
     DBFileInUse = (Err <> 0)
     Close #1
End Function

 

This tip is reprinted from the VB Tips & Tricks Volume 1 book.
Parts of this tip was submitted by: Grahm Jones

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s