Note that there are some explanatory texts on larger screens.

plurals
  1. POKeep getting error 2 (Registry path does not exist) with RegQueryValueEx in VBA
    primarykey
    data
    text
    <p>I'm trying to have VBA query the registry to see if an ODBC driver is installed, and I get error 2 when trying to read a key's value. I tried a simpler key/value, but no cigar.</p> <h2>Edit for Clarification</h2> <p>I am developing on a 32-bit system, but this needs to work for both 32 and 64-bit systems. What the problem is is that the call to RegOpenKeyEx works, but RegQueryValueEx returns error 2: file does not exist on my 32-bit system. My syntax appears correct, what am I doing wrong?</p> <pre class="lang-vb prettyprint-override"><code>Public Const HKEY_LOCAL_MACHINE = &amp;H80000002 Public Const ERROR_SUCCESS = 0&amp; ' Successful Public Const ERROR_FILE_NOT_FOUND = 2&amp; ' Registry path does not exist Public Const ERROR_ACCESS_DENIED = 5&amp; ' Requested permissions not available Public Const STANDARD_RIGHTS_READ = &amp;H20000 Public Const SYNCHRONIZE = &amp;H100000 Public Const KEY_ENUMERATE_SUB_KEYS = &amp;H8 Public Const KEY_NOTIFY = &amp;H10 Public Const KEY_QUERY_VALUE = &amp;H1 Public Const KEY_READ = ((STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE _ Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) _ And (Not SYNCHRONIZE)) Public Const REG_SZ = 1 ' Unicode nul terminated string Public Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" _ (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As _ Long, ByVal samDesired As Long, phkResult As Long) As Long Public Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" _ (ByVal hKey As Long, lpValueName As String, _ ByVal lpReserved As Long, lpType As Long, _ lpData As Any, lpcbData As Long) As Long Public Declare Function RegCloseKey Lib "advapi32.dll" _ (ByVal hKey As Long) As Long Sub TestRegAPI() Dim KeyName As String, handle As Long, handle2 As String 'KeyName = "SOFTWARE\ODBC\ODBCINST.INI\ODBC Drivers" KeyName = "SOFTWARE\7-Zip" r = RegOpenKeyEx(HKEY_LOCAL_MACHINE, KeyName, 0, KEY_READ, handle) If r Then MsgBox "Unable to open the specified Registry key, code " &amp; r Else 'r = RegQueryValueEx(handle, "MySQL ODBC 5.1 Driver", 0, 1&amp;, handle2, length) r = RegQueryValueEx(handle, "Path", 0, REG_SZ, handle2, length) RegCloseKey handle End If End Sub </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload