Windows Script function to read registry keys

Window registry contains the information about settings of system hardware and installed software and their associate values. The registry is organized in hierarchical branches structure and data values are stored as keys. In general it is not recommended to make changes to registry because programs and applications often make all the necessary changes automatically. Incorrect changes to the registry of your computer can cause the computer to stop working. However, if a corrupt file is listed in the registry, you may need to make changes. To identify, firstly we need to read the registry name and value. For that we are creating a function which will read the registry key and value from the registry database.

To read the registry database we are using the window script host method. It provides access to the native window shell. The object run a program locally and helps to manipulate the contents of registry. We are creating VBA function that will read the registry name and value, and show the value on MS Access message box. After implementing we have to pass the path in window scrip host object as shown in fig 1.1.

Read window registry key using VBA code. Fig-1.1


Read Function match the input registry name with registry database list, if the name matches then the value get printed as shown in fig 1.2.

Read window registry key using VBA code. Fig-1.2


And if not match then it will show not found message as shown in fig 1.3.

Read window registry key using VBA code. Fig-1.3


VBA code

Option Compare Database
Public function CHECK_REGKEY()
Dim RgtrKey As String
Dim VAlKey As String
Dim Outkey As Integer
RgtrKey = InputBox("Enter the name of registry key", "Registry Key")
If ClRgtrKey(RgtrKey) = True Then
VAlKey = RDRgtrKey(RgtrKey)
Outkey = MsgBox("The registry value for the key """ & RgtrKey & """" & vbCr & "is """ & VAlKey & """")
Outkey = MsgBox("Registry key """ & RgtrKey & """not found.")
End If
End function
Public Function ClRgtrKey(entr_RgtrKey As String) As Boolean
Dim objShell As Object
On Error GoTo ErrorHandler
Set objShell = CreateObject("WScript.Shell")
objShell.RegRead entr_RgtrKey
ClRgtrKey = True
Exit Function
ClRgtrKey = False
End Function
Public Function RDRgtrKey(entr_RgtrKey As String) As String
Dim objShell As Object
On Error Resume Next
Set objShell = CreateObject("WScript.Shell")
RDRgtrKey = objShell.RegRead(entr_RgtrKey)
End Function


It is advised that the information provided in the article should not be used for any kind formal or production programming purposes as content of the article may not be complete or well tested. ERP Makers will not be responsible for any kind of damage (monetary, time, personal or any other type) which may take place because of the usage of the content in the article.