Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2002
    Location
    California
    Posts
    32

    Unanswered: Windows Registry

    I am currently developing a front end databse for my company that consist of several (20+) files that it will access. Because this will be distributed to locations where I cannot personally oversee the installation (and they're doomed to get it wrong) I want my database to access the regestry to find values addded by the install program that specifys were the files were placed. I haved looked every where but could not find how to access the system registry from vba. Is it possible? If so, how?

  2. #2
    Join Date
    Dec 2002
    Location
    Préverenges, Switzerland
    Posts
    3,740
    oooops: misread the question!

  3. #3
    Join Date
    Dec 2001
    Posts
    79
    I've cut and pasted some pieces of code (untested) that should do the job for you. Much of this comes from the Access Developer's Handbook, but I've made a few modifications:

    Code:
    Option Compare Database
    Option Explicit
    Private Const adhcHKEY_CLASSES_ROOT = &H80000000
    Private Const adhcHKEY_CURRENT_USER = &H80000001
    Private Const adhcHKEY_LOCAL_MACHINE = &H80000002
    Private Const adhcHKEY_USERS = &H80000003
    Private Const adhcHKEY_PERFORMANCE_DATA = &H80000004
    
    ' Reg Key Security Options...
    Private Const READ_CONTROL = &H20000
    Private Const KEY_QUERY_VALUE = &H1
    Private Const KEY_SET_VALUE = &H2
    Private Const KEY_CREATE_SUB_KEY = &H4
    Private Const KEY_ENUMERATE_SUB_KEYS = &H8
    Private Const KEY_NOTIFY = &H10
    Private Const KEY_CREATE_LINK = &H20
    Private 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
                           
    Private Const STANDARD_RIGHTS_READ = &H20000
    Private Const SYNCHRONIZE = &H100000
    Private Const KEY_READ = ((STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And (Not SYNCHRONIZE))
    Private Const REG_SZ = 1
     
    Private Type SECURITY_ATTRIBUTES
        nLength As Long
        lpSecurityDescriptor As Long
        bInheritHandle As Long
    End Type
     
    Private 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
    
    Private Declare Function RegCloseKey Lib "advapi32" (ByVal hKey As Long) As Long
     
    Private Declare Function RegDeleteKey& Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal _
    hKey As Long, ByVal lpSubKey As String)
    
    Private Declare Function RegSetValueEx& Lib "advapi32.dll" Alias "RegSetValueExA" _
    (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal _
    dwType As Long, lpData As Any, ByVal cbData As Long)
    
    Private Declare Function RegQueryValueEx& Lib "advapi32.dll" Alias "RegQueryValueExA" _
    (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, _
    ByRef lpType As Long, ByVal lpData As Any, ByRef lpcbData As Long)
    
    Private Declare Function RegCreateKeyEx Lib "advapi32.dll" Alias "RegCreateKeyExA" _
    (ByVal hKey As Long, ByVal lpSubKey As String, ByVal Reserved As Long, ByVal _
    lpClass As String, ByVal dwOptions As Long, ByVal samDesired As Long, _
    lpSecurityAttributes As SECURITY_ATTRIBUTES, phkResult As Long, lpdwDisposition _
    As Long) As Long
    
    Function reg_GetLocalMachineRegKeyValueStr(strSubKey As String, strValueName As String) As String
    Dim l As Long
    Dim hKey As Long
    Dim KeyValType As Long
    Dim strTmpVal As String
    Dim KeyValSize As Long
    
    strTmpVal = ""
    
    '* Open specified reg key
    l = RegOpenKeyEx(adhcHKEY_LOCAL_MACHINE, strSubKey, 0, KEY_READ, hKey)
    If l = 0 Then
    Else
        'MsgBox "Error in reg_GetLocalMachineRegKeyValueStr (1): " & l
        GoTo Exit_Section
    End If
    
    strTmpVal = String$(1024, 0)
    KeyValSize = 1024
    
    '* Set Reg Key value
    l = RegQueryValueEx&(hKey, strValueName, 0&, KeyValType, strTmpVal, KeyValSize)
    If l = 0 Then
    Else
        'MsgBox "Error in reg_GetLocalMachineRegKeyValueStr (2): " & l
        GoTo Exit_Section
    End If
    
    'Win95 adds a null terminated string / NT does not
    'So, we strip it off below, if we need to
    
    If (Asc(Mid(strTmpVal, KeyValSize, 1)) = 0) Then
        strTmpVal = Left(strTmpVal, KeyValSize - 1)
    Else
        strTmpVal = Left(strTmpVal, KeyValSize)
    End If
    
    '* Close key
    l = RegCloseKey(hKey)
    If l = 0 Then
    Else
        MsgBox "Error in reg_GetLocalMachineRegKeyValueStr (9): " & l
        GoTo Exit_Section
    End If
    
    Exit_Section:
        reg_GetLocalMachineRegKeyValueStr = strTmpVal
    
    End Function
    Ex.:
    MsgBox reg_GetLocalMachineRegKeyValueStr("SOFTWARE\MySoft wareCompanyName\MyApplicationName", "WhateverRegKeyIWantToRetrieve")

    Hope this helps,
    Peter De Baets
    Peter's Software - Microsoft Access Tools for Developers
    http://www.peterssoftware.com

  4. #4
    Join Date
    Nov 2002
    Location
    San Francisco
    Posts
    251

    Re: Windows Registry

    I like to use Windows Scripting Host....

    Sub ReadRegistry()

    Set WshShell = CreateObject("WScript.Shell")
    MsgBox WshShell.RegRead("HKEY_CURRENT_USER\Software\micro soft\internet explorer\main\start page")

    End Sub



    see http://msdn.microsoft.com/library/de...mthregread.asp for details.....


    jiri

  5. #5
    Join Date
    Dec 2002
    Location
    California
    Posts
    32
    It worked almost perfectly the first time with little debugging. Thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •