Storing and retrieving information from the Windows Registry will play a major part in any programmer’s life. You can store small bits of information, such as recently used files, form sizes and location, and user preferences—to name just a few—in the Windows Registry. My aim with this article is to explain the major differences between the way Visual Basic 6 and Visual Basic .NET utilise the Windows Registry.
Visual Basic 6 and the Windows Registry
Visual Basic 6 comes with four procedures for accessing the Windows Registry:
SaveSetting
The SaveSetting statement is used to store a value in the Registry.
Syntax
SaveSetting VBKeyName, Section, Key, Setting
Term | Meaning |
---|---|
SaveSetting | Statement name |
VBKeyName | String value representing the name of the key within the VB/VBA area of the Registry |
Section | String value representing the section or subkey for the specific application’s setting |
Key | String value that represents the name of a specific entry within the section |
Setting | String value that you want to store to a given key |
Example
'Store Left Value SaveSetting App.Title, "Form Location", "Left", "250" 'Store Top Value SaveSetting App.Title, "Form Location", "Top", "300"
GetSetting
You use the GetSetting() function to retrieve a value from a particular section in the Visual Basic key of the Windows Registry.
Syntax
Term | Meaning |
---|---|
StringVariable | String value returned by the GetSetting() function |
GetSetting | The name of the function |
VBKeyName | String value representing the name of the key within the VB/VBA area of the Registry |
Section | String value representing the section or subkey for the specific applications setting |
Key | String value that represents the name of a specific entry within the section |
Default | Optional argument. A String value that represents the value to return if GetSetting() fails or encounters an error. If the function is successful, it returns the string found at the key setting; if it is unsuccessful, it returns the string value assigned to the Default argument |
Example
'Variable to hold value returned from the Registry Dim strTop As String 'Variable to hold value returned from Registry Dim strLeft As String 'Read Top value stored strTop = GetSetting(App.Title, "Form Location", "Top") 'Read Left value stored strLeft = GetSetting(App.Title, "Form Location", "Left")
GetAllSettings
You use GetAllSettings() to retrieve an array from the Registry that contains all the key settings along with their respective values of a particular section in the Registry.
Syntax
VariantVariable = GetAllSettings(VBKeyName, Section)
Term | Meaning |
---|---|
VariantVariable | An array of values returned by the function, of type Variant |
GetAllSettings | Function name |
VBKeyName | String value representing the name of the key within the VB/VBA area of the Registry |
Section | String value that represents the name of a specific entry within the section |
Example
Dim arrAllSettings As Variant 'Variable to store 2 dimensional 'array of values read from the 'Registry 'Retrieve Registry values stored under Form Location arrAllSettings = GetAllSettings(App.Title, "Form Location") 'Display the first key and first value under Form Location (Left) txtGetAllSettings.Text = arrAllSettings(0, 0) & " = " & _ arrAllSettings(0, 1) & vbCrLf 'Display the second key and second value under Form Location (Top) txtGetAllSettings.Text = txtGetAllSettings.Text & _ arrAllSettings(1, 0) _ & " = " & arrAllSettings(1, 1)
DeleteSetting
If you want to delete an entire section from a key, you use the DeleteSetting statement.
Syntax
DeleteSetting VBKeyName, Section [, Key]
Term | Meaning |
---|---|
DeleteSetting | Statement name |
VBKeyName | String value representing the name of the key within the VB/VBA area of the Registry |
Section | String Value representing the section to delete |
Key | String Value representing a specific subkey to delete. If you do not supply this optional paramater, all the subkeys of the section will be deleted |
Example
DeleteSetting App.Title, "Form Location" 'Delete Form Location
These internal Registry functions have one major drawback. Visual Basic can retrieve and store data only to a specific key in the Registry, namely:
MyComputerHKEY_CURRENT_USERSoftwareVB and VBA Program Settings
This process is, unfortunately, automatic to Visual Basic 6; Visual Basic 6 cannot write to or read from any other keys in the Windows Registry without the aid of the Windows API, functions such as:
- RegOpenKey
- RegOpenKeyEx
- RegCreateKey
- RegCreateKeyEx
- RegDeleteKey
- RegCloseKey
- RegQueryValueEx
- RegSetValueEx
- RegDeleteValue
- RegEnumKeyEx
- RegEnumValue
- RegRestoreKey
- RegSaveKey
Sample Application
The first sample application (RegistryVB6) accompanying this article shows in detail how the intrinsic Visual Basic 6 Registry functions are used to store and retrieve values in the Registry. The sample application also shows in detail how to use the necessary API functions to store values in the Registry and read from the Registry.
On the next page, I will explain how Visual Basic.NET accesses the Windows Registry.