Databases Reference
In-Depth Information
2.9
USING THE WINDOWS REGISTRY TO SAVE SETTINGS
In the advanced “browse for directory” example shown previously, there are numerous options that
can be set in the graphical user interface. The example is designed to start in the “simple” mode
with no initial directory set, and no options selected. Suppose, however, that it was desired to have
an application “remember” what its past settings were and how to automatically select those settings
upon startup. There are two means of accomplishing this task. The first is to store the settings prior
to exiting the program in a file and then to have the file read immediately upon executing the
program to determine what the initial settings should be set to. Such initialization (*.ini) files were
utilized for years under the Windows operating systems. This method has fallen into disfavor with
the advent of the Windows Registry. The Windows Registry is essentially a large database that
stores information on the programs that reside in the operating system. This section details how to
write information to, and read information from, the Windows Registry within the Excel VBA
framework.
The Windows Registry is not an easy application to access. There is no icon within Windows to
run the program, nor is there a link from the start menu. This was done on purpose to keep people
from tampering with the Windows Registry. Elements stored in the Windows Registry are called keys,
and if a key is deleted or changed to an invalid value, this can cause a program to become inoperable,
or, in the very worst case, disable the Windows operating system itself. Therefore, great care must
be taken when modifying elements in the Windows Registry. To access the Windows Registry, type
“regedit.exe” in the Run box under the Start menu as illustrated in Figure 2.12.
Elements in the Windows Registry are viewed in the registry editor much like files are viewed
in the Windows Explorer, except in this case the “files” are keys and they have a value associated
with them. The value associated with a key can be thought of as a variant because keys can contain
nearly any value such as string, integer, single, double, or Boolean. In VB and VBA, keys are stored
at this location within the Windows Registry:
HKEY_CURRENT_USER\Software\VB and VBA Program Settings\
Under the VB and VBA Program Settings folder, subfolders will be present. The subfolders are
denoted as Application Names, as these folders are where all the keys for a single application
should be stored. Folders beneath the Application Name folders are known as Sections, and different
Section subfolders can be utilized to file keys that are common to a certain aspect of an application.
For example, one section subfolder might hold the keys to one of many GUIs utilized in the
application. Applications and Sections follow the VB and VBA Program Settings folder as shown:
\VB and VBA Program Settings\Application Name\Section
The take-home message here is that, under the VB and VBA Program Settings location, there
can be numerous applications, each of which can have an Application Name subfolder. Each
application subfolder can have numerous section subfolders to hold groupings of related keys
utilized in the application.
FIGURE 2.12 Running the Windows Registry editor.
Search WWH ::




Custom Search