Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Accessing the registry from VBA!?

Posted on 1998-07-24
2
Medium Priority
?
589 Views
Last Modified: 2012-05-04
hi I tried to access the registry from VBA. I used the reg funktions from the "advapi32.dll".

the code:
Declare Function RegOpenKeyExA Biblio "ADVAPI32" _
        (ByVal hkey As Long; ByVal lpSubKey As String; _
        ByVal ulOptions As Long; ByVal samDesired As Long; phkResult As Long) _
        As Long
Declare Function RegQueryValueExA Biblio "ADVAPI32" _
        ByVal hkey As Long; ByVal lpValueName As String; _
        ByVal  lpReserved As Long; lpType As Long; ByVal lpData As String; _
        phkResult As Long) As Long
Declare Function RegCloseKey Biblio "ADVAPI32" _
        ByVal hkey As Long) As Long

Dim HKEY_LOCAL_MACHINE As Long
Dim KEY_ALL_ACCESS Als Lang
Dim phkErg As Long
Dim TypVar As Long
Dim langVar As Long
Dim Ergebnis2 As String
    'Evaluated from VC++:
    HKEY_LOCAL_MACHINE = -2147483646
    KEY_ALL_ACCESS = 983103

    Erfolg = RegOpenKeyExA(HKEY_LOCAL_MACHINE; "SOFTWARE\HelloWorld\"; _
       0; KEY_ALL_ACCESS; phkErg)              
 
    Erfolg = RegQueryValueExA(phkErg; "Kanal" & I; 0; TypVar; Ergebnis2; langVar)
         
    Erfolg = RegCloseKey(phkErg)                

The key was opened correctly (returns 0). Also the RegQueryValueEx returns fine (returns 0). The langVar (parameter six) is correct returned. The TypVar parameter (parameter four) is correct returned. Only the Value is empty!!!!!!!!!

comments?
0
Comment
Question by:nil092297
2 Comments
 

Author Comment

by:nil092297
ID: 1466588
Edited text of question
0
 
LVL 1

Accepted Solution

by:
sstew earned 1400 total points
ID: 1466589
In your code, TypeVar is not defined.

Try the following:

Global Const REG_MULTI_SZ As Long = 7
Global Const REG_SZ As Long = 1
Global Const REG_DWORD As Long = 4
Global Const REG_BINARY As Long = 3
Global Const HKEY_CLASSES_ROOT = &H80000000
Global Const HKEY_CURRENT_USER = &H80000001
Global Const HKEY_LOCAL_MACHINE = &H80000002
Global Const HKEY_USERS = &H80000003
Global Const KEY_ALL_ACCESS = &H3F

Function RegQueryStringValue(ByVal hkey As Long, ByVal strValueName As String, strData As String) As Boolean
  Dim lResult As Long
  Dim lValueType As Variant
  Dim strBuf As String
  Dim lDataBufSize As Long
 
  RegQueryStringValue = False
  On Error GoTo 0
  ' Get length/data type
  lValueType = REG_SZ
  lResult = OSRegQueryValueEx(hkey, strValueName, 0&, lValueType, ByVal 0&, lDataBufSize)
  If lResult = ERROR_SUCCESS Then
    strBuf = String(lDataBufSize, " ")
    lResult = OSRegQueryValueEx(hkey, strValueName, 0&, lValueType, ByVal strBuf, lDataBufSize)
      If lResult = ERROR_SUCCESS Then
        RegQueryStringValue = True
        strData = StripTerminator(strBuf)
      End If
  End If
 
End Function

This code is a refined version of the code contained in Microsoft's Setupkit program on the VB5 disk.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question