Results 1 to 12 of 12
  1. #1
    rednoise is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2015
    Posts
    5

    How can I determine if a database file is already open


    Hello,

    I want to create a simple database to check if other databases files (accdr files in my case) are open and if not overwrite files from server.

    I can always try to overwrite and supress any error but I like doing things in the proper way.

  2. #2
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,974
    Look for the presence of a laccdb lock file in the target folder. If it exists, the file is in use.
    The only complication is if the file has been opened exclusively as that doesn't create a lock file.
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  3. #3
    rednoise is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2015
    Posts
    5
    Yeah, I thought of that option but I really want to know if there is another/better way because sometimes there might be orphan laccdb files. I was wondering if there is an access native method to do it.

  4. #4
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,974
    Not that I'm aware of. You can always try and delete the lock file if it exists.
    If the file is in use, you won't be able to delete it...so you'll need error handling to manage that.
    Which brings us full circle...I would just handle the original error caused when you try and overwrite a file in use - IIRC it's error 70
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  5. #5
    Join Date
    Apr 2017
    Posts
    1,679
    You can have for Main form an Open/Load event , which inserts user id (or some other identificator) into special table, and a Close/Unload event, which deletes user id (identificator) from this table. Then you can simply check the database using an ODBC query to check this file - when there are entries, then the database is in use.

  6. #6
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,974
    I was also going to mention that approach as I've used it in the past.
    However, if the database crashes or the user shuts down their PC without closing the app first, the logout code won't run leaving an orphan record behind.
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  7. #7
    Join Date
    Apr 2017
    Posts
    1,679
    Nobody is protected from user!

    I have seen enough cases where lock file exists, and in reality nobody uses the database!

  8. #8
    Minty is offline VIP
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    This code checks if an instance of the database is running; (I'll happily give credit where due for it if anyone knows the source - I found it somewhere on the interwebs)

    Code:
    Function IsDatabaseRunning(strDBName As String) As Boolean
        '   Function to check if a database is already running
        '   Accepts:
        '       The path and name of an Access database
        '   Returns:
        '       True if the database can't be opened (because it is already open)
        '       False if the database can be opened (because it is not already open)
        On Error GoTo E_Handle
        IsDatabaseRunning = True
        Dim db      As Database
        Set db = DBEngine(0).OpenDatabase(strDBName, True)
        IsDatabaseRunning = False
    fExit:
        On Error Resume Next
        db.Close
        Set db = Nothing
        Exit Function
    E_Handle:
        Select Case Err.Number
            Case 3704                                ' Database already opened
            Case 3045                                ' Database already open as well
            Case Else
                MsgBox "E_Handle  " & Err.Description, vbOKOnly + vbCritical, "Error: " & Err.Number
        End Select
        Resume fExit
    End Function
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  9. #9
    Join Date
    Apr 2017
    Posts
    1,679
    What when database is split (front- and back-end)? Any number of users can be connected to back-end, and I'm afraid it will not be locked? (But for front-end it will work for sure!)

    And to add to confusion - what to do with back-end as e.g. SQL Server database?

  10. #10
    rednoise is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2015
    Posts
    5
    Quote Originally Posted by Minty View Post
    This code checks if an instance of the database is running; (I'll happily give credit where due for it if anyone knows the source - I found it somewhere on the interwebs)

    Code:
    Function IsDatabaseRunning(strDBName As String) As Boolean
        '   Function to check if a database is already running
        '   Accepts:
        '       The path and name of an Access database
        '   Returns:
        '       True if the database can't be opened (because it is already open)
        '       False if the database can be opened (because it is not already open)
        On Error GoTo E_Handle
        IsDatabaseRunning = True
        Dim db      As Database
        Set db = DBEngine(0).OpenDatabase(strDBName, True)
        IsDatabaseRunning = False
    fExit:
        On Error Resume Next
        db.Close
        Set db = Nothing
        Exit Function
    E_Handle:
        Select Case Err.Number
            Case 3704                                ' Database already opened
            Case 3045                                ' Database already open as well
            Case Else
                MsgBox "E_Handle  " & Err.Description, vbOKOnly + vbCritical, "Error: " & Err.Number
        End Select
        Resume fExit
    End Function
    Thank you! it seems it's working fine.

    ridders52's approach looks good too.

    Thanks everyone for the answers.

  11. #11
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,974
    On behalf of us all, you're welcome.
    Notice that the code Minty uses also depends on detecting errors.
    Although you were keen to avoid that, it is a standard approach in this type of code.
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  12. #12
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,974
    Just found another solution which I'd forgotten about and which requires no allowance for errors.

    This checks whether its possible to open a database exclusively.
    If the answer is no, its already open. If the answer is yes, its not open so is safe to replace
    It works even if the database being checked is itself opened exclusively

    Code:
    Public Function CanBeOpenedExclusively(ByVal FullPath As String) As Boolean
    
    On Error GoTo Err_Handler
    
    
        Dim D As DAO.Database
        Dim P As DAO.PrivDBEngine
        
        Set P = New PrivDBEngine
        On Error Resume Next
        Set D = P(0).OpenDatabase(FullPath, True)
        CanBeOpenedExclusively = Not (D Is Nothing)
        Debug.Print CanBeOpenedExclusively
        
        Set D = Nothing
        Set P = Nothing
        
    Exit_Handler:
        Exit Function
    
    
    Err_Handler:
        MsgBox "Error " & Err.Number & " in  CanBeOpenedExclusively procedure : " & Err.Description
        Resume Exit_Handler
        
    End Function
    Hope that helps
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Open and log in to database through batch file
    By spacekowboy in forum Access
    Replies: 9
    Last Post: 11-02-2015, 03:22 PM
  2. Replies: 1
    Last Post: 10-01-2015, 09:14 AM
  3. Replies: 5
    Last Post: 11-04-2013, 11:39 AM
  4. database does not want to open an .mda file
    By Henriette50 in forum Access
    Replies: 3
    Last Post: 07-28-2013, 03:15 PM
  5. Replies: 2
    Last Post: 05-26-2011, 12:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums