Microsoft Access VBA can read and write to the Windows Registry by using WMI. In this article we are going to show you how easy it is to read 5 different types of Registry Values with VBA and WMI.
The Registry provided a solution for the problem caused by the proliferation of separate ini settings files which often contained conflicting settings. The Registry became the central repository for data and settings reducing conflicts and providing better organization for the operating system, hardware, services, and applications settings. Most changes made concerning a computer and software are stored in the Registry.
The Registry is useful for the Access developer for storing connection strings, user preferences, a last used list, default startup settings and more. Since the Registry is vital to the operating system, hardware, services, and software it contains a very large and varied amount of data. Within this vast amount of data are treasures of information to be found. So let us begin by examining how we can read data from the Registry.
At first glance the Registry may seem very complicated, but actually the registry contains just two main types of elements: Keys and Values. The Keys are very similar to folders and subfolders having a similar hierarchy of Keys and SubKeys. The Registry Values are Name and Data pairs which are very much like the Properties and their Values we use in VBA code.
The top level Registry Keys are organized into Root Keys also known as Hives. The Root Keys on my XP are:
- HKEY_CLASSES_ROOT
- HKEY_CURRENT_USER
- HKEY_LOCAL_MACHINE
- HKEY_USERS
- HKEY_CURRENT_CONFIG
The most common Registry Value Names and their Constants are:
- String: REG_SZ = 1
- Expanded String: REG_EXPAND_SZ = 2
- Binary: REG_BINARY = 3
- DWord: REG_DWORD = 4
- Multi-String: REG_MULTI_SZ = 7
I will not take the time to explain them all here since ample information about Registry Value Types can easily be found on the internet.
Before I forget I need to give the usual, but important, warning that making changes to the Registry can cause damage to your software and operating system so if you are smart you will back up the Registry before making changes. To do that click on Start, then select Run, enter “Regedit” without the quotes and then click “OK”. After Regedit opens click on File and then Export to export your Registry to a backup file. This will probably take a few minutes, but it is better than hours of tears because you lost your loved ones photos!
The Registry stores information in several different types and using WMI we can read all the types listed here. We will start by declaring the Root Key Constants at the beginning of the Module.
Option Compare Database
Option Explicit
Const HKEY_CLASSES_ROOT = &H80000000
Const HKEY_CURRENT_USER = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
Const HKEY_USERS = &H80000003
Const HKEY_CURRENT_CONFIG = &H80000005
Most of this first part of the procedures is familiar to you if you have been following our articles. However in these procedures we access a different WMI root. When we Set the objReg we use “\root\default:StdRegProv” instead of “\root\cimv2″.
The first example demonstrates how to read is the simple String Value.
Sub GetRegString()
Dim objReg As Object
Dim strComputer As String
Dim strKeyPath As String
Dim strValueName As String
Dim strValue
strComputer = "."
Set objReg = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\" _
& strComputer & "\root\default:StdRegProv")
strKeyPath = "SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\MSACCESS.EXE"
strValueName = "Path"
objReg.GetStringValue HKEY_LOCAL_MACHINE, strKeyPath, strValueName, strValue
Debug.Print "Microsoft Access Location: " & strValue
Set objReg = Nothing
End Sub
The next value to read is the often used DWord Value. In this procedure we use a Select Case construct to give meaning to the DWord value. In this case it is the Windows Automatic Updates setting.
Sub GetRegDWord()
Dim objReg As Object
Dim strComputer As String
Dim strKeyPath As String
Dim strValueName As String
Dim strDWordValue As String
Dim dwValue
strComputer = "."
Set objReg = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\" _
& strComputer & "\root\default:StdRegProv")
strKeyPath = "SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update"
strValueName = "AUOptions"
objReg.GetDWORDValue HKEY_LOCAL_MACHINE, strKeyPath, strValueName, dwValue
' If there is no DWord Value we want "Null" to be returned
strDWordValue = Nz(dwValue, "Null")
Select Case strDWordValue
Case "2"
strDWordValue = strDWordValue & " - Notify for download and notify for install."
Case "3"
strDWordValue = strDWordValue & " - Auto download and notify for install."
Case "4"
strDWordValue = strDWordValue & " - Auto download and schedule the install."
Case "Null"
strDWordValue = "Automatic Updates status is Unknown - No value was returned."
Case Else
strDWordValue = strDWordValue & " - Automatic Updates are not enabled."
End Select
Debug.Print strDWordValue
Set objReg = Nothing
End Sub
The next value to read is the Multi-String Value. In this procedure we will get a list of Event Log Sources. In a later article we will show how to filter and save specific Event Logs Text which contain important data.
' Example Call SaveRegSystemEventLog("C:\SystemEventLogSources.txt")
Sub SaveRegSystemEventLog(strTextFilePath As String)
Dim objReg As Object
Dim fso As Object
Dim fsoFile As Object
Dim strComputer As String
Dim strValueName As String
Dim strKeyPath As String
Dim arrValues
Dim arrValue
Set fso = CreateObject("Scripting.FileSystemObject")
Set fsoFile = fso.CreateTextFile(strTextFilePath)
strComputer = "."
Set objReg = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\" _
& strComputer & "\root\default:StdRegProv")
strKeyPath = "SYSTEM\CurrentControlSet\Services\Eventlog\System"
strValueName = "Sources"
objReg.GetMultiStringValue HKEY_LOCAL_MACHINE, strKeyPath, _
strValueName, arrValues
For Each arrValue In arrValues
fsoFile.WriteLine arrValue
' Debug.Print arrValue
Next
fsoFile.Close
Set fsoFile = Nothing
Set fso = Nothing
Set objReg = Nothing
End Sub
The next value to read is the Expanded String Value.
Sub ReadExpandedRegString()
Dim objReg As Object
Dim strComputer As String
Dim strValueName As String
Dim strKeyPath As String
Dim strValue
strComputer = "."
Set objReg = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\" _
& strComputer & "\root\default:StdRegProv")
strKeyPath = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\WinLogon"
strValueName = "UIHost"
objReg.GetExpandedStringValue HKEY_LOCAL_MACHINE, strKeyPath, _
strValueName, strValue
Debug.Print "The Windows logon UI host is: " & strValue
Set objReg = Nothing
End Sub
Lastly, we see the code to read a binary registry value. In this case the code reads the binary AccessName Value.
Sub ReadBinaryRegValue(strTextFilePath As String)
Dim objReg As Object
Dim fso As Object
Dim fsoFile As Object
Dim strComputer As String
Dim strKeyPath As String
Dim strValueName As String
Dim i As Long
Dim varValue
Set fso = CreateObject("Scripting.FileSystemObject")
Set fsoFile = fso.CreateTextFile(strTextFilePath)
strComputer = "."
Set objReg = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\" _
& strComputer & "\root\default:StdRegProv")
strKeyPath = "Software\Microsoft\Office\11.0\Access"
strValueName = "AccessName"
objReg.GetBinaryValue HKEY_CURRENT_USER, strKeyPath, _
strValueName, varValue
For i = LBound(varValue) To UBound(varValue)
fsoFile.WriteLine varValue(i)
Next
fsoFile.Close
Set fsoFile = Nothing
Set fso = Nothing
Set objReg = Nothing
End Sub
We will learn more about how VBA can manage the Registry using WMI in the articles to come.
Republished from Access Easy Tips [18 clicks].
Read the original version here [32134 clicks].