Maintain Custom Settings Using the Windows Registry

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

Maintain Custom Settings Using the Windows Registry

by Sean Kavanagh

Application: Microsoft Access 2000
Operating System: Microsoft Windows

As you strive to make your Access applications user-friendly, you'll probably find that you want certain aspects of the application to carry over from one session to the next. For instance, you may allow users to customize options in the application that you want to apply when the database or a particular form is opened. Your first instinct may be to store the settings' values in an Access table. However, you shouldn't overlook another resource you have that's designed with the idea of storing application settings in mind: the Windows registry.

In this article, we'll show you how easy it is to leverage the power of the registry in your Access applications. We'll show you how to create registry keys in a specific section of the registry, return the values for use in your applications, and delete registry keys when you no longer need them.

Overview

The registry is a database that stores the important system settings for your PC's hardware and software. The registry is organized in hierarchical branches, and data values are stored as keys. Through VBA, you can add and edit custom settings in a specific branch of the registry. There are four tools that VBA provides to let you manipulate custom registry keys: SaveSetting, GetSetting, DeleteSetting and GetAllSettings. These statements and functions allow you to work with registry settings that are saved in the HKEY_CURRENT_USER\Software\VB and VBA Program Settings branch of the registry.

Creating a simple example

To illustrate ways in which you can work with the registry, we'll build a simple example form that saves information to the registry. Let's say that you've created a contact-management application and you want to store the path to Word documents used for mail merges. In addition, we'll store the user's name, the default number of copies of a document he normally wants printed, and when the preferences were last updated. Using the finished example shown in Figure A as a guide, open a new form in Design view and add the textbox controls and command buttons shown. Name the controls txtPath, txtName, txtCopies, cmdSave and cmdDelete. Then, set the form's Caption property to Preferences and save the form as frmPreferences.

Figure A: We'll use this simple form to manipulate registry keys.

Saving information to the registry

You'll use the SaveSetting statement to create new registry keys or save information to an existing setting key you've already created. This statement uses the following syntax:

SaveSetting appname, section, key, setting

The *appname*argument is the name you want to call your application; however, the name doesn't have to be the same as your Access MDB. You'll save related keys to a specific section, defined by the *section*argument. The key argument is the name of the key you're creating or saving to. The previous three arguments are all required and are entered as String expressions. The final required argument, setting, is the actual value of the registry key you're saving.

Let's create the procedure that saves your form's information to the registry. First, click the Code button. Then, enter the procedure shown in Listing A. We're using the On Error Resume Next statement to ignore error 94, which occurs if you try to save a Null value to the registry. Of course, you'll want to employ more sophisticated error trapping in your applications.

Listing A: Code to save preferences

Private Sub cmdSave_Click()
On Error Resume Next
SaveSetting "My Contact Manager", _
    "Preferences", "Document Path", txtPath

SaveSetting "My Contact Manager", _
    "Preferences", "User Name", txtName

SaveSetting "My Contact Manager", _
    "Preferences", "Default Copies", txtCopies

SaveSetting "My Contact Manager", _
    "Preferences", "Preference Update", Now
End Sub

At this point, switch to Form view, enter some data, and click the Save button on your form. Although you won't see any indication, your data has been saved. Now, close your form and reopen it. Since the text boxes we used were unbound, the form is blank when you open it. Now, the trick is getting the information back out of the registry to populate the text boxes.

Retrieving the registry key values

Now that you know how to create and modify registry keys, let's take a look at how to get that data back out. To do so, you'll use the GetSetting() function. This function is written in the following format:

GetSetting(appname, section, key, default)

As you can see, it's quite similar to the SaveSetting statement. You must specify the appropriate appname, section and key arguments as String expressions. GetSetting() also lets you specify an optional default argument, which can be extremely helpful. The value used as the default argument will be used if an appname, section or key argument specified in the function doesn't exist. If you don't define the default argument, a zero-length string will be used.

To retrieve our registry key values, we'll attach code to the form's Load event. To do so, switch to Design view and click the Code button. Then, select Form from the Object dropdown list. At the insertion point, enter the code shown in Listing B. When you've finished, save and close the module. Then, switch to Form view. Your form is now populated with the appropriate information and the form's caption indicates the time of the last update, as shown in our example in Figure B.

Listing B: Code to retrieve values

txtPath = GetSetting("My Contact Manager", _
    "Preferences", "Document Path", "C:\My Documents\")

txtName = GetSetting("My Contact Manager", _
    "Preferences", "User Name")

txtCopies = GetSetting("My Contact Manager", _
    "Preferences", "Default Copies", 1)

Me.Caption = "Preferences - Last Updated: " & _
    GetSetting("My Contact Manager", "Preferences", _
    "Preference Update")

Figure B: We retrieved this data from the Windows registry.

Removing keys from the registry

To remove a registry key, you'll use the DeleteSetting statement, which takes this form:

DeleteSetting appname, section, key

This statement allows you to delete a specific key, an entire section, or all the registry keys for an application.

Note: Access' Help files, and several other resources, indicate that the section argument is required when you use DeleteSetting. This is probably due to problems VB 4.0 had with deleting nested keys. We've often used DeleteSetting with only the appname argument specified, without negative results.

Switch back to Design view, and then click the Close button so that we can incorporate the DeleteSetting statement into our form module. Press [Ctrl][End] to move to the end of the existing code. Then, enter the following statements:

Private Sub cmdDelete_Click()
On Error Resume Next
DeleteSetting "My Contact Manager"
End Sub

Now, switch back to Form view and click the Delete button. Again, you won't see any indication that a change has taken place. However, save and close the form, then reopen it. The previous values you entered are gone. But, you'll notice that txtPath and txtCopies do contain values because we specified a default argument in the GetSetting functions used to populate those controls.

Returning all the values from a registry section

Now that we've shown you the basics of adding, returning and deleting custom registry keys, let's take a look at the last registry function we mentioned, GetAllSettings(). This function returns an array of all the registry key names and values in a particular section of the registry. To see how to use this function, use frmPreferences to save new values to the registry. Then, close the form, switch to the Modules sheet of the Database window, and click the New button.

The GetAllSettings function uses this syntax:

GetAllSettings(appname, section)

We'll use the GetAllSettings() function to read all of the keys and values in our application's Preferences section and print them to the Debug window. To do so, enter the code shown in Listing C in the Module window.

Listing C: Return a list of registry keys and values

Sub MyApplicationSettings()
Dim varSettings As Variant
Dim x As Integer
varSettings = _
    GetAllSettings("My Contact Manager", "Preferences")

For x = LBound(varSettings, 1) To UBound(varSettings, 1)

    Debug.Print varSettings(x, 0), varSettings(x, 1)

Next
End Sub

To see the results of the MyApplicationSettings procedure, press [Ctrl]G to display the Immediate window. Then, type MyApplicationSettings and press [Enter] to produce a result resembling the one shown in Figure C.

Figure C: The GetAllSettings function returns a two-dimensional array of the keys and values in a particular registry section.

Conclusion

Although the thought of interacting with the registry may seem daunting, we've shown you some easy-to-use tools provided by Access that allow you to maintain custom registry entries. The registry is designed with the idea of storing user preferences in mind, so you shouldn't be afraid to take advantage of it.

Copyright © 2001 Element K Content LLC. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of Element K Content LLC is prohibited. Element K is a service mark of Element K LLC.