ACCESS VBA - check if workbook is open or not open (1 Viewer)

johnseito

Registered User.
Local time
Today, 08:17
Joined
Feb 27, 2013
Messages
89
is there any access VBA code to check if an workbook is open or not open ?

In excel VBA - you could just loop through all the excel files that are open and you find the name of the file while you loop through and you could tell if that file is open. I couldn't do this in Access VBA.
 

ashleedawg

"Here for a good time"
Local time
Today, 05:17
Joined
Jun 22, 2017
Messages
154
Code:
Sub IsDatabaseOpen()
    Dim appAccess As Access.Application
    Dim fileName As String, File_is_closed As Boolean
    fileName = "c:\path\filename.accdb"
    On Error Resume Next
    Set appAccess = GetObject(fileName)
    If Err.Number <> 0 Then File_is_closed = True
    Debug.Print "File Closed? " & File_is_closed
End Sub
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 20:17
Joined
May 7, 2009
Messages
19,233
same what you did in excel to check if it is open.
on a Module paste this:

Code:
Public Function IsWorkbookOpen(ByVal strWorkBookName As String) As Boolean
    Dim objExcel As Object
    Dim varWorkbook As Variant
On Error GoTo ExitFunction
    Set objExcel = GetObject(, "Excel.Application")
    For Each varWorkbook In objExcel.Workbooks
        If varWorkbook.Name = strWorkBookName Then
            IsWorkbookOpen = True
            Exit For
        End If
    Next
ExitFunction:
    Set objExcel = Nothing
End Function

To call:

debug.print IsWorkbookOpen("Text.xlsx")
 

johnseito

Registered User.
Local time
Today, 08:17
Joined
Feb 27, 2013
Messages
89
Ok- thank you both, that work. It's a bit different than excel VBA but I see how it is. Can't put the path and file name but just the file name.
 
Last edited:

Users who are viewing this thread

Top Bottom