2010-01-12 48 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

相關問題