使用 VBA,查找 Windows 中安装的 MySQL ODBC 驱动程序的版本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2050443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
With VBA, find the version of the MySQL ODBC driver installed in Windows
提问by Ben McCormack
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?
使用 Visual Basic for Applications,如何确定用户计算机上的 Windows 中安装了哪个版本的 MySQL ODBC 驱动程序?
I have a Microsoft Access application that uses the MySQL ODBC driver to make a connection. The connection string looks like this:
我有一个 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;
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.
直到 IT 经理在用户的 PC 上安装 MySQL ODBC 驱动程序的 5.1 版之前,这一直有效,这破坏了我的连接字符串。
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?
如果我知道用户的 Windows XP 安装上安装的驱动程序版本,我可以在运行时将其插入到连接字符串中。 如何使用 VBA 找出用户计算机上的 Windows 中安装了哪个版本的 MySQL ODBC 驱动程序?
回答by Re0sless
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)
使用此处找到的信息,您可以使用以下代码获取它(我在 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
回答by dcp
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]
1 您可以检查注册表并查找特定键,例如:[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
2.你可以查看他们的c:\windows\system32文件夹中是否有myodbc.dll,然后查看版本信息。这是有关如何检查版本的链接:http: //www.vb-helper.com/howto_file_version_info.html
回答by HaveAGuess
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)
此函数旨在枚举 ODBC 驱动程序的 regkeys 并检查某处是否存在 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
And you'll need this to enumerate the reg keys (for more on this see http://technet.microsoft.com/en-us/library/ee176771.aspx):
您将需要它来枚举 reg 键(有关更多信息,请参见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