I'm using the code below in Excel 365 > Visual Basic Editor to attempt to determine which version of MS Access was used to create an old database. The code works for a newer .accdb database I created as a test. As far as I know the older database was originally written/designed in Access 97, but I want to find out for sure.
Public Sub GetAccessFormat()
' Attempt to determine the format of an Access database
' Note: Can Access 16.0 (Office 365) read databases from Office 2013 and earlier?
On Error GoTo Error_NotAccessDatabase
Dim fileName As String
'fileName = "C:\Tmp\old.mdb" ' Fails
fileName = "C:\Tmp\new.accdb" ' Works
If IsEmpty(Dir(fileName)) Then
MsgBox "Could not find: " & fileName
Exit Sub
End If
' Open the database
Dim objAccess As Object
Set objAccess = CreateObject("Access.Application") ' Is this the problem?
objAccess.OpenCurrentDatabase fileName
objAccess.Visible = False
' Get the file format
Dim fileFormat As Integer
fileFormat = objAccess.CurrentProject.FileFormat ' Gets here and fails for .mdb (returns "12" for the .accdb)
Dim strAccessFormat As String
strAccessFormat = "Your database is: "
Select Case fileFormat
Case 2
strAccessFormat = (strAccessFormat & "Microsoft Access 2")
Case 7
strAccessFormat = (strAccessFormat & "Microsoft Access 95")
Case 8
strAccessFormat = (strAccessFormat & "Microsoft Access 97")
Case 9
strAccessFormat = (strAccessFormat & "Microsoft Access 2000")
Case 10
strAccessFormat = (strAccessFormat & "Microsoft Access 2002")
Case 11
strAccessFormat = (strAccessFormat & "Microsoft Access 2003")
Case 12
strAccessFormat = (strAccessFormat & "Microsoft Access 2007")
Case 14
strAccessFormat = (strAccessFormat & "Microsoft Access 2010")
Case 15
strAccessFormat = (strAccessFormat & "Microsoft Access 2013")
Case 16
strAccessFormat = (strAccessFormat & "Microsoft Access 2016/9")
Case Else
strAccessFormat = "Unknown Access file format"
End Select
' Close database and display the format information
objAccess.CloseCurrentDatabase
strAccessFormat = (strAccessFormat + " (" & fileFormat & ".0)")
MsgBox strAccessFormat
Exit Sub
Error_NotAccessDatabase:
' Unable to open the database (not Access or not supported by this version of Office?)
MsgBox "Unable to open as Access database: " & strFile & ", Error: " & Err.Description
Exit Sub
End Sub
The error text is "The expression you entered refers to an object that is closed or doesn't exist." (The database does exist, is not open in another application and there is no lock on the database)
We have Office 365 (with Access 16.0) installed, so I assume the problem is with Set objAccess = CreateObject("Access.Application") because the "Access Application" on my PC will be unable to read older format access databases.
Is this correct? And is there a workaround to determine the file format of old Access databases?