我正在使用下面Excel 365 >中的代码来尝试确定使用哪个版本的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数据库的文件格式?
发布于 2021-03-03 20:41:20
您还可以“窥视”文件的第二十一字节,并从中确定其文件类型:
Function GetAccessFileType(filePath As String) As String
Dim strm As New ADODB.Stream
strm.Type = adTypeBinary
strm.Open
strm.LoadFromFile filePath
strm.Read 20
Dim bytes As Variant
bytes = strm.Read(1)
Dim fileTypeNumber As Integer
fileTypeNumber = CInt(bytes(0))
strm.Close
Set strm = Nothing
Dim fileTypeString As String
Select Case fileTypeNumber
Case 0:
fileTypeString = "Access 97 or older"
Case 1:
fileTypeString = "Access 2000/2003"
Case 2:
fileTypeString = "Access 2007"
Case 3:
fileTypeString = "Access 2010"
Case 5:
fileTypeString = "Access 2016 with BIGINT support (Type 5)"
Case Else:
fileTypeString = "Unknown (" & fileTypeNumber & ")"
End Select
GetAccessFileType = fileTypeString
End Function发布于 2021-03-03 18:59:02
您通常不希望在测试版本时使用完整的胖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://learn.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 = ???但是,对于这个版本,我还没有准备好解码器。对于在没有最近字段的情况下创建的accdb (没有bigint/datetime扩展),它返回"12.0",这与最近使用的2010年特性一致。
然而,如果我添加一个日期时间扩展字段,它会跳转到"32.0",这很难解码,特别是从2016年开始,数据库功能还没有与主要Office版本相对应( 2016年初版本不支持bigint,2016年后期版本支持)。
发布于 2021-03-03 18:29:21
据微软称:
将Access 97数据库转换为.accdb格式
从Access 2013开始,不再可能直接将Access 97数据库(.mdb)转换为.accdb文件格式。但是,您可以在Access的先前版本中打开Access 97数据库,然后以Access 2013可以打开的格式保存数据库。例如,下面是如何使用Access 2003打开Access 97数据库,然后将其转换为Access 2013可以打开的格式:
中打开Access 97数据库。
”
还可以使用Access 2007或Access 2010将Access 97数据库转换为.accdb格式。在这两种产品中打开Access 97数据库时,数据库增强向导将帮助将数据库转换为.accdb格式。
替代解决方案:
如果您没有旧版本的Access,并且希望提取数据,可以使用任何现代Excel版本,使用在data选项卡上找到的导入函数。一旦将数据保存到Excel中,就可以将其导入Excel以从Excel访问。
测试代码:
尽管如此,这里是您的代码的测试版本,它适用于Access可以打开的任何DB:
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 Subhttps://stackoverflow.com/questions/66462193
复制相似问题