read dsn info from reg

Discussion in 'Microsoft Access Getting Started' started by mcnews, Mar 3, 2011.

  1. mcnews

    mcnews Guest

    how to read user dsn regkey created by DBEngine.RegisterDatabase?
    need to retrieve so user can see it.
    tia,
    mcnewsxp
     
    mcnews, Mar 3, 2011
    #1
    1. Advertisements

  2. mcnews

    mcnewsxp Guest

    i figured it out if anyone wants to know.
     
    mcnewsxp, Mar 3, 2011
    #2
    1. Advertisements

  3. "mcnewsxp" <> wrote

    > i figured it out if anyone wants to know.


    Why don't you share it, just in case someone does? I'm sure it's
    information that I might sometime find useful.

    Larry Linson
    Microsoft Office Access MVP
     
    Access Developer, Mar 4, 2011
    #3
  4. mcnews

    mcnewsxp Guest

    here's what i used. if you want to look up other HKEYs you'll need to find
    the appropriate value.

    Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" _
    (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As
    Long
    Private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias
    "RegQueryValueExA" _
    (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As
    Long, lpType As Long, ByVal lpData As String, _
    lpcbData As Long) As Long
    Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long)
    As Long

    Public Const HKEY_CURRENT_USER = &H80000001
    Public Const REG_BINARY As Long = 3
    Public Const REG_DWORD As Long = 4

    Public Function ReadRegistry(ByVal Group As Long, ByVal Section As String,
    ByVal Key As String) As String

    ''''''use this somewhere appropriate in your app to get your value
    ' sVal = ReadRegistry(HKEY_CURRENT_USER, "Software\ODBC\ODBC.INI\PHINMS",
    "Server")

    Dim lResult As Long, lKeyValue As Long, lDataTypeValue As Long,
    lValueLength As Long, sValue As String, td As Double
    Dim TStr1 As String, TStr2 As String, i As Integer
    On Error Resume Next
    lResult = RegOpenKey(Group, Section, lKeyValue)
    sValue = Space$(2048)
    lValueLength = Len(sValue)
    lResult = RegQueryValueEx(lKeyValue, Key, 0&, lDataTypeValue, sValue,
    lValueLength)
    If (lResult = 0) And (Err.Number = 0) Then
    If lDataTypeValue = REG_DWORD Then
    td = Asc(Mid$(sValue, 1, 1)) _
    + &H100& * Asc(Mid$(sValue, 2, 1)) + &H10000 *
    Asc(Mid$(sValue, 3, 1)) + &H1000000 _
    * CDbl(Asc(Mid$(sValue, 4, 1)))
    sValue = Format$(td, "000")
    End If
    If lDataTypeValue = REG_BINARY Then
    ' Return a binary field as a hex string (2 chars per byte)
    TStr2 = ""
    For i = 1 To lValueLength
    TStr1 = Hex(Asc(Mid(sValue, i, 1)))
    If Len(TStr1) = 1 Then TStr1 = "0" & TStr1
    TStr2 = TStr2 + TStr1
    Next
    sValue = TStr2
    Else
    sValue = Left$(sValue, lValueLength - 1)
    End If
    Else
    sValue = "Not Found"
    End If
    lResult = RegCloseKey(lKeyValue)
    ReadRegistry = sValue
    End Function
     
    mcnewsxp, Mar 4, 2011
    #4
  5. Thanks for posting this for us. I've saved it off -- never can tell when you
    might find it useful.

    Larry Linson
    Microsoft Office Access MVP

    "mcnewsxp" <> wrote in message
    news:ikqlvc$tgk$-september.org...
    > here's what i used. if you want to look up other HKEYs you'll need to
    > find the appropriate value.
    >
    > Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA"
    > _
    > (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As
    > Long
    > Private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias
    > "RegQueryValueExA" _
    > (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As
    > Long, lpType As Long, ByVal lpData As String, _
    > lpcbData As Long) As Long
    > Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As
    > Long) As Long
    >
    > Public Const HKEY_CURRENT_USER = &H80000001
    > Public Const REG_BINARY As Long = 3
    > Public Const REG_DWORD As Long = 4
    >
    > Public Function ReadRegistry(ByVal Group As Long, ByVal Section As String,
    > ByVal Key As String) As String
    >
    > ''''''use this somewhere appropriate in your app to get your value
    > ' sVal = ReadRegistry(HKEY_CURRENT_USER, "Software\ODBC\ODBC.INI\PHINMS",
    > "Server")
    >
    > Dim lResult As Long, lKeyValue As Long, lDataTypeValue As Long,
    > lValueLength As Long, sValue As String, td As Double
    > Dim TStr1 As String, TStr2 As String, i As Integer
    > On Error Resume Next
    > lResult = RegOpenKey(Group, Section, lKeyValue)
    > sValue = Space$(2048)
    > lValueLength = Len(sValue)
    > lResult = RegQueryValueEx(lKeyValue, Key, 0&, lDataTypeValue, sValue,
    > lValueLength)
    > If (lResult = 0) And (Err.Number = 0) Then
    > If lDataTypeValue = REG_DWORD Then
    > td = Asc(Mid$(sValue, 1, 1)) _
    > + &H100& * Asc(Mid$(sValue, 2, 1)) + &H10000 *
    > Asc(Mid$(sValue, 3, 1)) + &H1000000 _
    > * CDbl(Asc(Mid$(sValue, 4, 1)))
    > sValue = Format$(td, "000")
    > End If
    > If lDataTypeValue = REG_BINARY Then
    > ' Return a binary field as a hex string (2 chars per byte)
    > TStr2 = ""
    > For i = 1 To lValueLength
    > TStr1 = Hex(Asc(Mid(sValue, i, 1)))
    > If Len(TStr1) = 1 Then TStr1 = "0" & TStr1
    > TStr2 = TStr2 + TStr1
    > Next
    > sValue = TStr2
    > Else
    > sValue = Left$(sValue, lValueLength - 1)
    > End If
    > Else
    > sValue = "Not Found"
    > End If
    > lResult = RegCloseKey(lKeyValue)
    > ReadRegistry = sValue
    > End Function
    >
     
    Access Developer, Mar 4, 2011
    #5
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Monte B

    Run time error '3260' - unable to enter training info

    Monte B, Jul 24, 2003, in forum: Microsoft Access Getting Started
    Replies:
    0
    Views:
    768
    Monte B
    Jul 24, 2003
  2. Bradley C. Hammerstrom

    Get info: Number of records in tables

    Bradley C. Hammerstrom, Sep 4, 2003, in forum: Microsoft Access Getting Started
    Replies:
    2
    Views:
    162
    Bradley C. Hammerstrom
    Sep 5, 2003
  3. mcnews

    Create DSN with UID and PWD

    mcnews, Apr 12, 2006, in forum: Microsoft Access Getting Started
    Replies:
    1
    Views:
    515
    Douglas J Steele
    Apr 12, 2006
  4. Isis

    Connecting a second 'front end' to a DSN

    Isis, Jun 11, 2008, in forum: Microsoft Access Getting Started
    Replies:
    3
    Views:
    193
  5. Jo Singer

    How can I edit linked tables in access to change the DSN?

    Jo Singer, Aug 27, 2009, in forum: Microsoft Access Getting Started
    Replies:
    1
    Views:
    1,053
    John W. Vinson
    Aug 28, 2009
Loading...

Share This Page