Find Version of Access

前端 未结 4 1334
北海茫月
北海茫月 2021-01-20 12:21

I have code below which determines the version of Access. It runs quickly on most PCs. We also have four terminal servers. On two of the terminal servers it runs fine. On th

4条回答
  •  一向
    一向 (楼主)
    2021-01-20 13:00

    I think the problem is the call to CreateObject(). This will run up Access which I guess can take 15 seconds on some machines. Here’s a alternative way to get the version number which should be a lot faster – it uses the information in the registery.

    Imports Microsoft.Win32
    
    Public Class AccessInterop
        Public Shared Function GetAccessVersionNiceName() As String
            Try
                Dim ClassName As String = GetAccessClassName()
                Select Case GetAccessVersionNumber(ClassName)
                    Case 8
                        Return "Access 97"
                    Case 9
                        Return "Access 2000"
                    Case 10
                        Return "Access XP"
                    Case 11
                        Return "Access 2003"
                    Case 12
                        Return "Access 2007"
                    Case 13
                        Return "Access 2010"
                    Case Else
                        Return "unknown"
                End Select
            Catch ex As Exception
                Return "unknown"
            End Try
        End Function
    
        Private Shared Function GetAccessClassName() As String
            Dim RegKey As RegistryKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey("Access.Application\CurVer")
            If RegKey Is Nothing Then
                Throw New ApplicationException("Can not find MS Access version number in registry")
            Else
                Return RegKey.GetValue("")
            End If
        End Function
    
        Public Shared Function GetAccessVersionNumber(ByVal ClassName As String) As Integer
            Dim VersionNumber As String = ClassName
            While VersionNumber.IndexOf(".") > -1
                VersionNumber = VersionNumber.Substring(VersionNumber.IndexOf(".") + 1)
            End While
            Return VersionNumber.Trim
        End Function
    End Class
    

提交回复
热议问题