使用VBA查找Windows中安装的MySQL ODBC驱动程序的版本

发布于 2024-08-17 09:53:31 字数 525 浏览 2 评论 0原文

使用 Visual Basic for Applications,如何查明用户计算机上的 Windows 中安装了哪个版本的 MySQL ODBC 驱动程序?

我有一个 Microsoft Access 应用程序,它使用 MySQL ODBC 驱动程序进行连接。连接字符串如下所示:

ODBC;DATABASE=mydatabase;DRIVER={MySQL ODBC 3.51 Driver};
    OPTION=3;PWD=password;PORT=3306;SERVER=server-db;UID=db-user;

这是工作查找,直到 IT 经理在用户 PC 上安装了 5.1 版 MySQL ODBC 驱动程序,这破坏了我的连接字符串。

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

始终不够 2024-08-24 09:53:32

您可以在注册表中

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

使用此处找到的信息找到它,您可以获取它使用下面的代码(我在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

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
ˇ宁静的妩媚 2024-08-24 09:53:32

以下是一些可能的想法:

1 您可以检查注册表并查找特定键,例如: [HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBCINST.INI\MySQL ODBC 3.51 Driver]

2.您可以检查他们的 c :\windows\system32文件夹下找到myodbc.dll,然后查看版本信息。这是有关如何检查版本的链接:
http://www.vb-helper.com/howto_file_version_info.html

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

太阳男子 2024-08-24 09:53:32

如果您想避免根据具体情况处理版本,您可以迭代键值,例如..

此函数旨在枚举 ODBC 驱动程序的注册表键,并仅检查某处是否存在 mysql,如果不存在,它将警告用户,然后将他们带到下载页面,并提醒他们获取适合其架构的正确版本 (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

并且您将需要它来枚举注册表项(有关更多信息,请参阅 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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文