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

Ben*_*ack 7 mysql ms-access odbc vba connection-string

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 a Microsoft Access application that uses the MySQL ODBC driver to make a connection. The connection string looks like this:

ODBC;DATABASE=mydatabase;DRIVER={MySQL ODBC 3.51 Driver};
    OPTION=3;PWD=password;PORT=3306;SERVER=server-db;UID=db-user;
Run Code Online (Sandbox Code Playgroud)

This was working find until the IT manager installed version 5.1 of the MySQL ODBC driver on a user's PC, which broke my connection string.

如果我知道在用户的Windows XP安装上安装了驱动程序的版本,我可以在运行时将其插入到连接字符串中. 如何使用VBA在用户的计算机上找出Windows中安装的MySQL ODBC驱动程序版本?

Re0*_*ess 14

您可以在注册表中找到它

HKEY_LOCAL_MACHINE\SOFTWARE\
    ODBC\ODBCINST.INI\
    ODBC Drivers\MySQL ODBC 3.51 Driver


 HKEY_LOCAL_MACHINE\SOFTWARE\
    ODBC\ODBCINST.INI\
    ODBC Drivers\MySQL ODBC 5.1 Driver
Run Code Online (Sandbox Code Playgroud)

使用此处的信息,您可以使用以下代码(我在Access 97中测试它)获取它

Private Sub Command0_Click()    
    If RegKeyExists("HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBCINST.INI\
                                 ODBC Drivers\MySQL ODBC 3.51 Driver") Then
        MsgBox "3.51"
    ElseIf RegKeyExists("HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBCINST.INI\
                                 ODBC Drivers\MySQL ODBC 5.1 Driver") Then
        MsgBox "5.1"
    Else
        MsgBox "None"
    End If
End Sub


'returns True if the registry key i_RegKey was found
'and False if not
Function RegKeyExists(i_RegKey As String) As Boolean
    Dim myWS As Object

    On Error GoTo ErrorHandler
    'access Windows scripting
    Set myWS = CreateObject("WScript.Shell")
    'try to read the registry key
    myWS.RegRead i_RegKey
    'key was found
    RegKeyExists = True
    Exit Function

ErrorHandler:
  'key was not found
  RegKeyExists = False
End Function
Run Code Online (Sandbox Code Playgroud)