Newsgroup: it.comp.lang.visual-basic
Titolo: verificare collegamento odbc

ciao
come posso verificare se un collegamento odbc esiste o meno?
grazie

 grazie
'Inserisci questo codice nel form load iniziale (all'apertura del programma)
Dim Valore As String
GetKeyValue HKEY_LOCAL_MACHINE, "SOFTWARE\ODBC\ODBC.INI\NomeConnessioneODBC
", "Description", Valore
If Valore = "" Then msgbox "Connessione esistente"
'Inserisci questo invece in un modulo:
Option Explicit
Public Const READ_CONTROL = &H20000
Public Const KEY_QUERY_VALUE = &H1
Public Const KEY_SET_VALUE = &H2
Public Const KEY_CREATE_SUB_KEY = &H4
Public Const KEY_ENUMERATE_SUB_KEYS = &H8
Public Const KEY_NOTIFY = &H10
Public Const KEY_CREATE_LINK = &H20
Public Const KEY_ALL_ACCESS = KEY_QUERY_VALUE + KEY_SET_VALUE + _
                       KEY_CREATE_SUB_KEY + KEY_ENUMERATE_SUB_KEYS + _
                       KEY_NOTIFY + KEY_CREATE_LINK + READ_CONTROL
Public Const HKEY_CLASSES_ROOT = &H80000000
Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const HKEY_CURRENT_CONFIG = &H80000005
Public Const HKEY_CURRENT_USER = &H80000001
Public Const HKEY_DYN_DATA = &H80000006
Public Const HKEY_PERFORMANCE_DATA = &H80000004
Public Const HKEY_USERS = &H80000003
Public Const ERROR_SUCCESS = 0
Public Const REG_SZ = 1
Public Const REG_DWORD = 4
Public Declare Function RegOpenKeyEx Lib "advapi32" Alias "RegOpenKeyExA"
(ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, _
ByVal samDesired As Long, ByRef phkResult As Long) As Long
Public Declare Function RegQueryValueEx Lib "advapi32" Alias _
"RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal _
lpReserved As Long, ByRef lpType As Long, ByVal lpData As String, ByRef _
lpcbData As Long) As Long
Public Declare Function RegCloseKey Lib "advapi32" (ByVal hKey As Long) As _
Long
Public Function GetKeyValue(KeyRoot As Long, KeyName As String, SubKeyRef As
_
String, ByRef KeyVal As String) As Boolean
    Dim i As Long                                           ' Loop Counter
    Dim rc As Long                                          ' Return Code
    Dim hKey As Long                                        ' Handle To An
    Dim hDepth As Long                                      '
    Dim KeyValType As Long                                  ' Data Type Of A
    Dim tmpVal As String                                    ' Tempory
    Dim KeyValSize As Long                                  ' Size Of
    '------------------------------------------------------------
    ' Open RegKey Under KeyRoot {HKEY_LOCAL_MACHINE...}
    '------------------------------------------------------------
    rc = RegOpenKeyEx(KeyRoot, KeyName, 0, KEY_ALL_ACCESS, hKey) ' Open
    If (rc <> ERROR_SUCCESS) Then GoTo GetKeyError          ' Handle
    tmpVal = String$(1024, 0)                             ' Allocate
    KeyValSize = 1024                                       ' Mark Variable
    '------------------------------------------------------------
    ' Retrieve Registry Key Value...
    '------------------------------------------------------------
    rc = RegQueryValueEx(hKey, SubKeyRef, 0, _
                         KeyValType, tmpVal, KeyValSize)    ' Get/Create Key
    If (rc <> ERROR_SUCCESS) Then GoTo GetKeyError          ' Handle Errors
    If (Asc(Mid(tmpVal, KeyValSize, 1)) = 0) Then           ' Win95 Adds
        tmpVal = Left(tmpVal, KeyValSize - 1)               ' Null Found,
    Else                                                    ' WinNT Does NOT
        tmpVal = Left(tmpVal, KeyValSize)                   ' Null Not
    End If
    '------------------------------------------------------------
    ' Determine Key Value Type For Conversion...
    '------------------------------------------------------------
    Select Case KeyValType                                  ' Search Data
    Case REG_SZ                                             ' String
        KeyVal = tmpVal                                     ' Copy String
    Case REG_DWORD                                          ' Double Word
        For i = Len(tmpVal) To 1 Step -1                    ' Convert Each
            KeyVal = KeyVal + Hex(Asc(Mid(tmpVal, i, 1)))   ' Build Value
        Next
        KeyVal = Format$("&h" + KeyVal)                     ' Convert Double
    End Select
    GetKeyValue = True                                      ' Return Success
    rc = RegCloseKey(hKey)                                  ' Close Registry
    Exit Function                                           ' Exit
GetKeyError:      ' Cleanup After An Error Has Occured...
    KeyVal = ""                                             ' Set Return Val
    GetKeyValue = False                                     ' Return Failure
    rc = RegCloseKey(hKey)                                  ' Close Registry
End Function
Ciao Marco

Dim Valore As String
GetKeyValue HKEY_LOCAL_MACHINE,  <--- Errore:Argomento nn facoltativo!
"SOFTWARE\ODBC\ODBC.INI\NomeConnessioneODBC
Come mai? Grazie.


 ha scritto nel messaggio
news:3dde46a0...@powernews.libero.it...

Hai ragione ho dimenticato di inserire tra le virgolette la descrizione
della connessione ODBC.
Sarà quindi così:
Dim Valore As String
GetKeyValue HKEY_LOCAL_MACHINE, "SOFTWARE\ODBC\ODBC.INI\NomeConnessioneODBC
", "Description", Valore
If Valore = "Descrizione Origine dati" Then msgbox "Connessione esistente"
La descrizione origine dati la trovi nel pannello di controllo alla sezione
ODBC cliccando sulla connessione di tuo interesse.
Spero di essere stato chiaro, se dovessi avere problemi contattami
Ciao
P.S. Ricordati che Outlook Express mi ha mandato a capo la riga di codice,
da GetKeyValue a Valore è tutto sulla stessa riga.