views:

882

answers:

2

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;

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.

If I knew the version of the driver installed on the user's Windows XP installation, I could insert that into the connection string at run-time. How can I find out which version of the MySQL ODBC driver is installed in Windows on a user's machine using VBA?

+3  A: 

You can find it in the registry under

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

Using the info found here, you can get at it using the below code (I tested it in 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
Re0sless
How can I use VBA to look at the registry at the location you specified?
Ben McCormack
You will find script here http://stackoverflow.com/questions/2020181/find-version-of-access/2020919#2020919 for checking the registry that will work in Access.
Remou
That script uses VB.NET, which doesn't always translate to VBA. How will it work?
Ben McCormack
@Re0sless That worked incredibly well. Much thanks!
Ben McCormack
Just a thought but the function checks the key exists, not the value of the key, I don't know if the key is uninstalled with the driver. so you may with to check the value of the key too. there is a function for that in the link in my answer.
Re0sless
@Ben McCormick 'That script' does not use VB.Net, it uses VBScript, and works fine in Access. Or at least it did when I put it together :)
Remou
The script is not using VBScript, but VBA, since it will be run within Access. As written, it would require substantial editing to run as a VBScript.
David-W-Fenton
@DW Fenton, if you are referring to my comment, the script I refer to is in the link I posted as a comment, at that time the post above did not have any code at all. The script in the link is VBScript, I tested in both Access and VBScript when I was putting it together.
Remou
OK -- I missed tracing things back. Probably not a good idea to use something as ambiguous as "that script" in a comment, don't you think?
David-W-Fenton
+2  A: 

Here are a few possible ideas:

1 You may be able to check the registry and look for specific keys, like this for example: [HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBCINST.INI\MySQL ODBC 3.51 Driver]

2.You could check their c:\windows\system32 folder for the myodbc.dll, and then check the version information. Here's a link on how to check the version: http://www.vb-helper.com/howto_file_version_info.html

dcp