With VBA, find the version of the MySQL ODBC driver installed in Windows

后端 未结 3 771
独厮守ぢ
独厮守ぢ 2021-01-12 10:49

Using Visual Basic for Applications, how can I find out which version of the MySQL ODBC driver is installed in Windows on a user\'s machine?

I have

3条回答
  •  天命终不由人
    2021-01-12 11:46

    If you want to avoid handling versions on a case by case basis you can iterate through the key values, eg..

    This function is designed to enumerate through the regkeys for ODBC drivers and just check for the existance of mysql somewhere, if not it will warn the user then take them to the download page, and remind them to get the right version for their architecture (32/64)

    Public Function CheckMySQL()
    
        Dim arrEntryNames()
        Dim arrValueTypes()
        Dim rPath As String
        rPath = "SOFTWARE\ODBC\ODBCINST.INI\ODBC Drivers"
    
        Call EnumerateRegEntries(rPath, arrEntryNames, arrValueTypes)
    
        If Not IsEmpty(arrEntryNames) Then
            For Each strAsk In arrEntryNames
                If (InStr(strAsk, "MySQL")) Then
                    strFound = strFound & strAsk & ", "
                End If
            Next
        End If
    
        If (Len(strFound) = 0) Then
            #If Win64 Then
                MsgBox "You need MySQL Driver *64 bit* - Press OK to get it!"
            #Else
                MsgBox "You need MySQL Driver *32 bit* - Press OK to get it!"
            #End If
    
            ActiveWorkbook.FollowHyperlink Address:="http://goo.gl/vbm6g", NewWindow:=True
    
            CheckMySQL = False
        Else
            CheckMySQL = True
        End If
    
    End Function
    

    And you'll need this to enumerate the reg keys (for more on this see http://technet.microsoft.com/en-us/library/ee176771.aspx):

    Public Sub EnumerateRegEntries(strKeyPath, arrEntryNames, arrValueTypes)
        Const HKEY_CLASSES_ROOT = &H80000000&
        Const HKEY_CURRENT_USER = &H80000001&
        Const HKEY_LOCAL_MACHINE = &H80000002&
        Const HKEY_USERS = &H80000003&
        Const HKEY_CURRENT_CONFIG = &H80000005&
    
        Dim objReg As Object
        Dim strComputer As String
    
        strComputer = "."
        Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
        strComputer & "\root\default:StdRegProv")
    
        objReg.EnumValues HKEY_LOCAL_MACHINE, strKeyPath, arrEntryNames, arrValueTypes
    
    
    End Sub
    

提交回复
热议问题