2010-01-12 81 views
7

使用Visual Basic for Applications,我怎样才能知道哪个版本的MySQL ODBC驱动程序在Windows中安装在用户的机器上?使用VBA,查找安装在Windows中的MySQL ODBC驱动程序的版本

我有一个使用MySQL ODBC驱动程序进行连接的Microsoft Access应用程序。连接字符串看起来是这样的:

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

这是努力的过程,直到IT经理安装MySQL的ODBC驱动程序5.1版用户的电脑,它打破了我的连接字符串。

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

回答

13

你可以找到它在注册表中

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 

使用发现here的信息,您可以使用下面的代码得到它(我在访问测试了它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 
+0

如何使用VBA在您指定的位置查看注册表? – 2010-01-12 16:36:11

+0

您将在这里http://stackoverflow.com/questions/2020181/find-version-of-access/2020919#2020919检查将在工作访问注册表找到脚本。 – Fionnuala 2010-01-12 16:43:45

+0

该脚本使用VB.NET,并不总是转换为VBA。它将如何工作? – 2010-01-12 16:53:51

4

以下是一些可能的想法:

1您可能能够检查注册表并查找特定的键,例如:[HKEY_LOCAL_MACHINE \ SOFTWAR E \ ODBC \ ODBCINST.INI \ MySQL ODBC 3.51驱动程序]

2.您可以检查它们的c:\ windows \ system32文件夹中的myodbc.dll,然后检查版本信息。下面是关于如何检查版本的链接:如果你想避免逐个您可以通过键值迭代,比如操控性上的情况下版本 http://www.vb-helper.com/howto_file_version_info.html

1

..

该功能的目的是枚举通过对REGKEYS 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 

你需要这个来枚举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 
+1

注:'ActiveWorkbook.FollowHyperlink'是用于Excel。 FollowHyperlink在Access中正常工作。 提示:更新的代码返回找到正确的字符串,如: '如果(InStr函数(strAsk, “MySQL的”)> 0,InStr函数(strAsk, “统一”)> 0),则 strFound = strAsk modLog.Log LOGNAME,“Found matching MySQL Driver:”&strFound,LOG_INFO End If' and 'Else CheckMySQL = strFound End If ' – rangoy 2016-09-20 10:11:31

相关问题