ACC1x: How to Determine If Database Is Open Exclusively (1.x)

ID: Q114837


The information in this article applies to:


SUMMARY

This article describes a method using Access Basic that you can use to determine if a database is open exclusively in Microsoft Access version 1.x.

This article assumes that you are familiar with Access Basic and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information on Access Basic, please refer to the "Introduction to Programming" manual.


MORE INFORMATION

When a database is open exclusively, no other program or process can access that database. When a database is open shared, other programs and processes can get shared access to the database. This article demonstrates how to use the Open statement in Access Basic to determine if a database is open exclusively by using the Open statement's Shared argument to try to access the database file in shared mode. If the database is open exclusively, an error will occur. Trapping for the error determines whether the database is open exclusively or not.

This function may be useful to Access Basic programmers who want to warn their applications' users that a database is open in the wrong mode.

The following steps demonstrate how to determine whether a database is open exclusively:

  1. Open the sample database NWIND.MDB.


  2. Create a new module and add the following line to the Declarations section:
    
          Option Explicit 


  3. Enter the following function in the module:
    
          Function IsCurDBExclusive1x () As Integer
          ' Purpose: Determine if a database is open exclusively.
          ' Returns: 0 if the database is not open exclusively.
          '          -1 if the database is open exclusively.
          '          Err if any error condition is detected.
    
            Dim hFile As Integer
            Dim DBName As String
            hFile = FreeFile
    
            'This is the name of the database to test.
            dbname = "C:\access\nwind.mdb"
    
            If Dir$(DBName) <> "" Then
              On Error Resume Next
                Open dbname For Binary Access Read Write Shared As hFile
                  Select Case Err
                    Case 0
                      IsCurDBExclusive1x = False
                    Case 70
                      IsCurDBExclusive1x = True
                    Case Else:
                      IsCurDBExclusive1x = Err
                  End Select
                Close hFile
              On Error GoTo 0
            Else
              MsgBox "Couldn't find " & dbname & "."
            End If
          End Function 

    NOTE: Set the variable DBName to the name of the database file that you want to test.


  4. From the View menu, choose Immediate Window.


  5. Type the following line in the Immediate window, and then press ENTER:

    If IsCurDBExclusive1x()=True Then Msgbox "It's Exclusive!"


If the database named in the DBName variable is open exclusively, the message "It's Exclusive!" will appear. If the database is open shared, the message will not appear.


REFERENCES

Microsoft Access "Language Reference," version 1.x, pages 343-345

For more information about accomplishing the same task in later versions of Microsoft Access, please see the following article in the Microsoft Knowledge Base:

Q117539 ACC: How to Determine If Database Is Open Exclusively


Keywords          : kbusage GnlMu 
Version           : 1.0 1.1
Platform          : WINDOWS 
Issue type        : kbhowto 

Last Reviewed: April 3, 1999