Accessing the Registry with VB6 and VB.NET | CodeGuru

Accessing the Registry with VB6 and VB.NET

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 […]

Written By
CodeGuru Staff
CodeGuru Staff
Jul 14, 2006
3 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

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"
Advertisement

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)
Advertisement

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:

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.

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.