问题描述
我在 Excel 365 > Visual Basic 编辑器中使用以下代码来尝试确定使用哪个版本的 MS Access 创建旧数据库。该代码适用于我作为测试创建的较新的 .accdb
数据库。据我所知,旧数据库最初是在 Access 97 中编写/设计的,但我想确定一下。
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
错误文本是“您输入的表达式引用了一个已关闭或不存在的对象。” (数据库确实存在,没有在其他应用程序中打开,并且数据库没有锁)
我们安装了 Office 365(带有 Access 16.0),所以我认为问题出在 Set objAccess = CreateObject("Access.Application")
上,因为我 PC 上的“Access 应用程序”将无法读取旧格式的 Access 数据库。
这是正确的吗?是否有解决方法来确定旧 Access 数据库的文件格式?
解决方法
您还可以“查看”文件的第 21 个字节并从中确定其文件类型:
null
,
您通常不想使用完整的 Access.Application
对象来测试版本,请尝试使用 DAO.DbEngine
对象:
' Open the database
Dim dbe As Object
If fileName Like "*.mdb" Then
Set dbe = CreateObject("DAO.DbEngine.36")
'Old JET engine,on 32-bits likely the MDAC version that still supports Access 2.0
Else
Set dbe = CreateObject("DAO.DbEngine.120")
'ACE engine
End If
Dim db As Object
Set db = dbe.OpenDatabase(fileName)
' Get the file format
Dim fileFormat As String
fileFormat = db.Version 'Decode: https://docs.microsoft.com/en-us/office/client-developer/access/desktop-database-reference/database-version-property-dao
请注意,CurrentProject.FileFormat
和 Database.Version
并没有真正超过 12.0。
我们也可以尝试获得更准确的版本:
fileFormat = db.Properties!Version 'Decode = ???
然而,对于这个版本,我没有准备好解码器。对于没有最近字段(没有 bigint/datetime 扩展)创建的 accdb,它返回“12.0”,这与最近使用的 2010 特性一致。
然而,如果我添加一个日期时间扩展字段,它会跳转到“32.0”,这更难解码,尤其是从 2016 年开始,数据库功能与主要的 Office 版本不对应(2016 年初的版本不支持bigint,2016 年末构建可以)。
,据微软称:
将 Access 97 数据库转换为 .accdb 格式
从 Access 2013 开始,不再可以直接转换 将 Access 97 数据库 (.mdb) 转换为 .accdb 文件格式。然而,你 可以在以前版本的 Access 中打开 Access 97 数据库,并且 然后以 Access 2013 可以打开的格式保存数据库。为了 例如,以下是如何使用 Access 2003 打开 Access 97 数据库, 然后将其转换为 Access 2013 可以打开的格式:
-
在 Access 2003 中打开 Access 97 数据库。
-
单击工具 > 数据库实用程序 > 转换数据库 > 以访问 2002-2003 文件格式。
-
输入数据库的名称并点击保存。
-
关闭 Access 2003 并打开 Access 2013。
-
打开数据库并单击文件 > 另存为 > Access 数据库 (.accdb) > 另存为 > 保存。
您还可以使用 Access 2007 或 Access 2010 来转换 Access 97 数据库到 .accdb 格式。当您打开 Access 97 数据库时 这两个产品中的任何一个,都会出现数据库增强向导 帮助将数据库转换为 .accdb 格式。
替代解决方案:
如果您没有旧版本的 Access 并且想要提取数据,您可以使用任何现代 Excel 版本使用“数据”选项卡上的导入功能执行此操作。将数据保存到 Excel 后,您可以将其从 Excel 导入回 Access。
测试代码:
综上所述,这里是您的代码的测试版本,适用于 Access 可以打开的任何数据库:
Public Sub GetAccessFormat()
' Attempt to determine the format of an Access database
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
Application.OpenCurrentDatabase fileName
Application.Visible = False
' Get the file format
Dim strAccessFormat As String
strAccessFormat = "Your database is: "
Select Case Application.CurrentProject.FileFormat
Case acFileFormatAccess2
strAccessFormat = (strAccessFormat & "Microsoft Access 2")
Case acFileFormatAccess95
strAccessFormat = (strAccessFormat & "Microsoft Access 95")
Case acFileFormatAccess97
strAccessFormat = (strAccessFormat & "Microsoft Access 97")
Case acFileFormatAccess2000
strAccessFormat = (strAccessFormat & "Microsoft Access 2000")
Case acFileFormatAccess2002
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
strAccessFormat = (strAccessFormat + " (" & Application.CurrentProject.FileFormat & ".0)")
MsgBox strAccessFormat
Application.CloseCurrentDatabase
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: " & fileName & ",Error: " & Err.Description
Exit Sub
End Sub