2010-01-12 29 views
7

Utilizzo di Visual Basic per applicazioni, come individuare la versione del driver ODBC MySQL installata in Windows sul computer di un utente?Con VBA, trova la versione del driver ODBC MySQL installato in Windows

Ho un'applicazione di Microsoft Access che utilizza il driver ODBC MySQL per effettuare una connessione. La stringa di connessione simile a questa:

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

Questa è stata scoperta a lavorare fino a quando il responsabile IT installata la versione 5.1 del driver ODBC di MySQL su PC di un utente, che ha rotto la mia stringa di connessione.

Se sapessi la versione del driver installato su un'installazione di Windows XP dell'utente, potrei inserire che nella stringa di connessione in fase di esecuzione. Come posso scoprire quale versione del driver MySQL MySQL è installata su Windows sul computer di un utente tramite VBA?

risposta

13

Lo si può trovare nel Registro di sistema

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 

utilizzando le informazioni trovato here, si può arrivare a esso usando il codice seguente (ho provato 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 
+0

Come posso utilizzare VBA per controllare il registro nella posizione specificata? –

+0

Troverete sceneggiatura qui http://stackoverflow.com/questions/2020181/find-version-of-access/2020919#2020919 per il controllo del Registro di sistema che funziona in Access. – Fionnuala

+0

Questo script utilizza VB.NET, che non sempre viene convertito in VBA. Come funzionerà? –

4

Ecco alcune idee possibili:

1 Potrebbe essere possibile controllare il registro e cercare chiavi specifiche, come ad esempio: [HKEY_LOCAL_MACHINE \ SOFTWAR E \ ODBC \ ODBCINST.INI \ MySQL ODBC 3.51 Driver]

2.È possibile controllare la cartella c: \ windows \ system32 per myodbc.dll, quindi controllare le informazioni sulla versione. Ecco un link su come controllare la versione: http://www.vb-helper.com/howto_file_version_info.html

1

Se si vuole evitare la manipolazione versioni su un caso per caso è possibile scorrere i valori chiave, ad esempio ..

Questa funzione è progettata per enumerare attraverso i regkeys per i driver ODBC e basta controllare l'esistenza di mysql da qualche parte, altrimenti avviserà l'utente portandoli alla pagina di download e ricorderà loro di ottenere la versione giusta per la loro architettura (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 

E avrete bisogno di questo per enumerare le chiavi reg (per maggiori informazioni su questo vedere 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

Nota: 'ActiveWorkbook.FollowHyperlink' è per excel. 'FollowHyperlink' funziona bene in Access. Suggerimento: aggiornare il codice per restituire la stringa corretta trovata, ad esempio: 'If (InStr (strAsk," MySQL ")> 0 And InStr (strAsk," Unicode ")> 0) Quindi strFound = strAsk modLog.Log LOGNAME, "Trovato corrispondenza driver MySQL:" & strFound, LOG_INFO Fine If' e 'Else CheckMySQL = strFound End If ' – rangoy