January 27, 2021
Hot Topics:

Using the Registry to Persist Data

  • By John Percival
  • Send Email »
  • More Articles »


We must have a function to remove entries in the registry. It is the DeleteSetting function:

DeleteSetting appname[, section[, key]]

This function is used to remove keys and sections. If you want to remove a key, you must specify the key and section parameters. To remove a whole section, you must specify the section parameter. The sub sections and sub keys are automatically deleted as well - you do not need to delete each individual key and section. To delete the entire application's data, just specify the appname.

For example:

DeleteSetting "Logon Master", "Users\User0", "Name"
' This will delete the Name key in 
' "Logon Master\Users\User0"
DeleteSetting "Logon Master", "Users"
' This will delete all sections in 
' "Logon Master\Users"
DeleteSetting "Logon Master"
' This will delete everything under "Logon Master"


This function will, as its name suggests, retrieve all keys in a certain section.

GetAllSettings(appname, section)

The parameters are as they are for all the functions. However, what this function does is more complicated. This function returns an array of variants containing the names and values of all the keys. The best way to explain this is by example:

' Variant to hold 2-dimensional 
' array returned by GetAllSettings
' Integer to hold counter.
Dim MySettings As Variant, intSettings As Integer

' Place some settings in the registry.
SaveSetting "Logon Master", "Users\User0", _
"Name", "John"
SaveSetting "Logon Master", "Users\User0", _
"Priviledges", "Supervisor"

' Retrieve the settings.
MySettings = GetAllSettings("Logon Master", _

' Display the settings
For intSettings = LBound(MySettings, 1) _
To UBound(MySettings, 1)

' MySettings(x,0) contains the name of the key, 
' and MySettings(x,1) contains the value
Debug.Print MySettings(intSettings, 0), _
MySettings(intSettings, 1)
Next intSettings

DeleteSetting "Logon Master", vbNullChar

Note: GetAllSettings does not return sub-sections, only keys.

Well that just about sums up the intrinsic VB registry functions. In the net article, we will look deeper at the registry, using API functions, and how it actually works, rather than from just VB's view point. We will see that VB is actually saving the data in "HKEY_CURRENT_USER\Software\VB and VBA Program Settings...". We will see how to access the rest of the registry.

As ever, be careful before poking round the registry with regedit.exe. It is easy to make mistakes and inadvertantly wipe most of the data, and all of the functionality of your machine. Be careful!

Page 2 of 2

This article was originally published on November 20, 2002

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date