Accessing the Registry With VB Functions

Although Visual Basic does include some built-in functions for manipulating the System Registry, the extent of what you can do with these functions is somewhat limited. Basically, they only let you access one part of the Registry. In addition, they restrict the number of levels of subkeys you can create.

The VB Registry functions always access the path HKEY_CURRENT_USER\Software\VB and VBA Program Settings\. You can create subkeys from this path, but the VB functions limit you to this specific part of the Registry. If you want access to other areas, you'll have to use Windows API functions, which are discussed in the next section of this chapter.

There are a total of four VB functions and statements for accessing the Registry. They are listed in Table 32.5. Note that in Visual Basic, a key is sometimes referred to as a setting.

Table 32.5. The Visual Basic functions for accessing the System Registry.

Function/Statement

Description

SaveSetting

Adds or updates keys and their values

GetSetting

Retrieves a key value

GetAllSettings

Retrieves a list of keys and their values

DeleteSetting

Deletes a subkey or key

These functions and statements are discussed in more detail in the sections that follow.

The SaveSetting Statement

The SaveSetting statement updates a key value. It can also create new keys. Its syntax is:

SaveSetting appname, section, key, setting

The appname argument specifies the name of the application to which the key belongs. The section argument specifies the subkey, key specifies the name of the key, and setting gives the value of the key.

If you were to use the SaveSetting statement in a program called MyCompany Demo and you wanted to save screen settings such as background color, the code might look like this:

SaveSetting "MyCompany Demo", "Screen", "BGColor", "&HFFFFFF"

This would result in a key with the following path being created in the Registry:

HKEY_CURRENT_USER\Software\VB and VBA Program Settings\MyCompany Demo\Screen\BGColor

Of course, if the key already existed, then the new value ("&HFFFFFF") would be assigned to the key.

The GetSetting Function

The GetSetting function does the opposite of the SaveSetting statement. It retrieves a key value from the Registry. Its syntax is:

GetSetting(appname, section, key[, default])

Again, the appname, section, and key arguments specify the application name, subkey name, and key name. The optional default argument is used to specify a value that will be returned by the function if the key cannot be located. If no default is given, the default return value is assumed to be an empty string ("").

If you had used the example code given for the GetSetting statement above, then the following line of code:

strValue = GetSetting("MyCompany Demo", "Screen", _
    "BGColor", "&H000000")

would result in the value "&HFFFFFF" being assigned to strValue. If the key did not exist, then strValue would be assigned "&H000000".

The GetAllSettings Function

If you want to retrieve all of the keys and their values in a particular subkey, you can use the GetAllSettings function. Its syntax is:

GetAllSettings(appname, section)

As before, the appname argument specifies the application name and the section argument specifies the name of the subkey. The value returned by the function is a Variant that is a two-dimensional array of strings containing all of the keys and their values in the subkey.

The DeleteSetting Statement

To delete a key or subkey from the Registry, use the DeleteSetting statement. Its syntax is:

DeleteSetting appname, section[, key]

Once again, the appname argument specifies the application name and the section argument specifies the name of the subkey. If you leave out the optional key argument, the subkey will be deleted. If you do specify a key, then only that key will be deleted.

Top Home