Language: Deutsch English















Last Update: 2024 - 05 - 21








Windows Registry Reader/Writer VBA Class Module

by Philipp Stiefel, originally published 2023-05-25

last revision on 2023-05-25


Old filing cabinetts - Article header image for VBA Registry Class Module

Photo by Geri Forsaith on Unsplash

The Windows Registry is Windows’ internal database to store a plethora of application and operating system settings. Sooner or later, you’ll have the requirement to read and/or write to the Windows Registry, and so did I.

There are a lot of half-baked implementations to read and write data from/to the Windows Registry in VB(A). – Here is yet another one now. 😇

I don’t claim that my class module is any better than other solutions that are available. – It’s probably worse, because I implemented only a very small subset of Registry operations. - However, it at least supports 64bit VBA, which many others do not. Also, it is “actively maintained”, which means I will add functionality to it if I myself need it. If you find a bug or need an enhancement to the module, I might feel inclined to add/fix it. - No guarantee, but chances are not too bad.

For now, this is class module supports …

  • Creating Registry Keys and Values,
  • Deleting Registry Keys and Values, and
  • Reading and writing String (REG_SZ) and Number (DWORD) data,

Download and Import

You can download it here: Registry Class Module

To use this class module in your own project you …

  • extract the file Registry.cls from the downloaded ZIP,
  • open the VBA environment in your Access project,
  • in the VBA environment, click menu “File” – “Import File” and select the Registry.cls file,
  • Done. - The class module is ready for use.

The above steps are required, instead of simple copy&paste from the .cls file, because the class module has a predeclared id set. This means that you can use a default instance of this class without explicitly creating an instance.

Registry Demo Code

For your convenience I wrote a small demo procedure that shows what the Registry class module can do and how you use it. This demo code should be self-explanatory, and the usage of class module should be straight-forward.

Public Sub DemoRegistryClassModule() Const KEY_NAME As String = "HKEY_CURRENT_USER\RegistryClassModuleTest" Const VALUE_NAME_DWORD = "TestValueNumeric" Const VALUE_NAME_STRING = "TestValueString" Const VALUE_DWORD = "123456" Const VALUE_STRING = "TestData" Registry.CreateKey KEY_NAME If Registry.KeyExists(KEY_NAME) Then Debug.Print "Key " & KEY_NAME & " was created" End If Registry.WriteString KEY_NAME, VALUE_STRING, VALUE_NAME_STRING Registry.WriteValue KEY_NAME, VALUE_DWORD, VALUE_NAME_DWORD Dim ValueFromRegistry As Variant ValueFromRegistry = Registry.ReadValue(KEY_NAME, VALUE_NAME_STRING) Debug.Print "Read string from Registry: " & ValueFromRegistry ValueFromRegistry = Registry.ReadValue(KEY_NAME, VALUE_NAME_DWORD) Debug.Print "Read number (DWORD) from Registry: " & ValueFromRegistry Registry.DeleteValue KEY_NAME, VALUE_NAME_STRING Registry.DeleteValue KEY_NAME, VALUE_NAME_DWORD Registry.DeleteKey KEY_NAME If Not Registry.KeyExists(KEY_NAME) Then Debug.Print "Key " & KEY_NAME & " no longer exists" End If End Sub

This demo procedure basically showed off (almost) all that this class module can do.

Fun Fact

To wrap things up, a little fun fact about this class module. I originally wrote this class module in 2001. – About 22 years ago.

I recently had the need to add a missing feature. So, I dug out the old code, wrote a couple of unit tests for it, and then made the required changes to add the feature. After I had the tests in place, I also had the confidence to quickly make the 64bit compatibility changes without fear of breaking things.

Even though the class module was used in numerous projects in the past 22 years, the newly written unit tests quickly exposed two bugs, which were easy fix once discovered. – Beyond that, the original code has aged pretty well.

Now it is public for you to enjoy too. 😁

Share this article: Share on Facebook Tweet Share on LinkedIn Share on XING

Subscribe to my newsletter

*

I will never share your email with anyone. You can unsubscribe any time.
This email list is hosted at Mailchimp in the United States. See our privacy policy for further details.

Benefits of the newsletter subscription





© 1999 - 2024 by Philipp Stiefel - Privacy Policiy