Procedure Name | Type | Description |
(Declarations) | Declarations | Declarations and private variables for the modRegistry module. |
RegistryCreateNewKey | Procedure | Create a new key in the system registry using RegCreateKeyEx. |
RegistryDeleteKey | Procedure | Delete a key from the system registry using RegDeleteKey. Note that any subkeys must be deleted first. The API
documentation for this function is incorrect. The Microsoft Knowledge Base states: "The documentation for RegDeleteKey() points out that the specified key to be deleted must not have subkeys. If the key to be deleted does have subkeys, RegDeleteKey() will fail with access denied. This happens despite the fact that the machine account has delete privileges and the registry handle passed to RegDeleteKey() was opened with delete access. The additional requirement is that the key must have no subkeys." |
RegistryDeleteValue | Procedure | Delete a value from the system registry using RegOpenKeyEx and RegDeleteValue. |
RegistryEnumerateSubKeys | Procedure | Enumerate the subkeys of the specified key using RegQueryInfoKey and RegEnumKey. |
RegistryEnumerateNames | Procedure | Enumerate the names of the values in a specified key using RegQueryInfoKey and RegEnumValue. |
RegistryEnumerateValues | Procedure | Get the names and values of a specified key using RegQueryInfoKey and RegEnumValue. |
RegistryGetKeyValue | Procedure | Get a value from the system registry using commands like RegQueryValueExNULL, RegQueryValueExString, and RegQueryValueExLong. |
RegistryGetKeyValueEitherRoot | Procedure | Get a value from the system registry by looking in one root (Machine or CurrentUser) and if not found, choosing the other one. |
RegistrySetKeyValue | Procedure | Set a registry key value and creates it if it doesn't already exist using RegCreateKeyEx API call. |
' Example of modRegistry ' ' To use this example, create a new module and paste this code into it. ' Then run the procedures by putting the cursor in the procedure and pressing: ' F5 to run it, or ' F8 to step through it line-by-line (see the Debug menu for more options) Private Sub Example_modRegistry_Read() ' Comments: Example of using the modRegistry module to read values from the Windows Registry with support for VBA with 32 and 64-bit API calls ' See registry values in the Immediate Window Dim strValue As String Dim astrKeys() As String Dim astrNames() As String Dim avarValues() As Variant Dim lngCount As Long Dim lngCounter As Long Dim eRootKeyFound As EnumRegistryRootKeys Dim strHive As String ' Get the location of the Windows Program Files strValue = RegistryGetKeyValue(HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows\CurrentVersion", "ProgramFilesDir") MsgBox "Location of Windows Program Files: " & strValue ' Search from either root (starting with User) to see if the value exists ' This example shows that after not finding it in the USER root, the value is found in the MACHINE root strValue = RegistryGetKeyValueEitherRoot(False, "SOFTWARE\Microsoft\Windows\CurrentVersion", "ProgramFilesDir", eRootKeyFound) If Len(strValue) > 0 Then If eRootKeyFound = HKEY_LOCAL_MACHINE Then strHive = "HKEY_LOCAL_MACHINE" ElseIf eRootKeyFound = HKEY_CURRENT_USER Then strHive = "HKEY_CURRENT_USER" End If MsgBox "Location of Windows Program Files: " & strValue & vbCrLf & _ "Found in registry hive: " & strHive End If ' This example shows that a name that doesn't exist returns a blank value strValue = RegistryGetKeyValue(HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows\CurrentVersion", "NonExistent") If Len(strValue) = 0 Then MsgBox "Value could not be found" End If ' Enumerate the subkeys by placing their values into an array, then print the values to the Immediate Window Call RegistryEnumerateSubKeys(HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Office", astrKeys(), lngCount) If lngCount > 0 Then Debug.Print "Microsoft Office SubKeys:" For lngCounter = 0 To lngCount - 1 Debug.Print astrKeys(lngCounter) Next lngCounter Debug.Print End If ' Get the names in a registry key, then print them to the Immediate Window Call RegistryEnumerateNames(HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows\CurrentVersion", astrNames(), lngCount) If lngCount > 0 Then Debug.Print "Microsoft Windows CurrentVersion Registry Names:" For lngCounter = 0 To lngCount - 1 Debug.Print astrNames(lngCounter) Next lngCounter End If Debug.Print ' Get the names and values of a registry subkey, then print them to the Immediate Window Call RegistryEnumerateValues(HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows\CurrentVersion", astrNames(), avarValues(), lngCount) If lngCount > 0 Then Debug.Print "Microsoft Windows CurrentVersion Registry Names and Values:" For lngCounter = 0 To lngCount - 1 Debug.Print astrNames(lngCounter), avarValues(lngCounter) Next lngCounter End If End Sub Private Sub Example_modRegistry_Write() ' Comments: Example of using the modRegistry module to create, get and delete values from the Windows Registry with support for VBA with 32 and 64-bit API calls ' See values in the Immediate Window Const cstrRegKey As String = "Software\FMS\TotalVisualSourceBook\Test" Const cstrRegValue As String = "TestValue" Dim strResult As String ' Creates a new key in the user's registry If RegistryCreateNewKey(HKEY_CURRENT_USER, cstrRegKey) Then MsgBox cstrRegKey & " successfully created", vbInformation ' Modify an existing key by assigning it the value of "SampleData" If RegistrySetKeyValue(HKEY_CURRENT_USER, cstrRegKey, cstrRegValue, "SampleData", REG_SZ) Then MsgBox cstrRegKey & " successfully set", vbInformation ' Get the value we just created strResult = RegistryGetKeyValue(HKEY_CURRENT_USER, cstrRegKey, cstrRegValue) Debug.Print "Key value: " & strResult & vbCrLf & "Retrieved from registry key: " & cstrRegKey ' Delete this value back out If RegistryDeleteValue(HKEY_CURRENT_USER, cstrRegKey, cstrRegValue) Then ' Now remove the key If RegistryDeleteKey(HKEY_CURRENT_USER, cstrRegKey) Then MsgBox cstrRegKey & " successfully deleted", vbInformation Else MsgBox cstrRegKey & " could not be deleted", vbCritical End If End If Else MsgBox cstrRegKey & " could not be set", vbCritical End If Else MsgBox cstrRegKey & " could not be created", vbCritical End If End Sub
The source code in Total Visual Sourcebook includes modules and classes for Microsoft Access, Visual Basic 6 (VB6), and Visual Basic for Applications (VBA) developers. Easily add this professionally written, tested, and documented royalty-free code into your applications to simplify your application development efforts.
Total Visual SourceBook is written for the needs of a developer using a source code library covering the many challenges you face. Countless developers over the years have told us they learned some or much of their development skills and tricks from our code. You can too!
Supports Access/Office 2016, 2013, 2010 and 2007, and Visual Basic 6.0!
"The code is exactly how I would like to write code and the algorithms used are very efficient and well-documented."
Van T. Dinh, Microsoft MVP