20.2. Using the Built-In VBA Registry Functions

Many programmers use global variables to hold values that are used throughout the application. There are two problems associated with this approach. First, if an unhandled error occurs, all your global variables are reset. Second, you have to reassign their values every time the application is launched. An alternative is to store this type of value in the database, but if you're storing the connection string to the remote data store, it might be a little difficult to get at if your application doesn't know where to look.

Another alternative, one that is used in most professional applications, is to store such information in the Registry. You can store all sorts of information in the Registry, from simple values that your applications use from time to time, to connection strings, to user preferences, such as the position and color of forms, and so on.

VBA provides four native functions for manipulating the Registry within VB and VBA. The sole drawback to these functions (if you want to call it a drawback) is that they operate on only one part of the Registry—one that has been specifically allocated to VB and VBA.

The HKEY_CURRENT_USER\Software\VB and VBA Program Settings key has been set aside for our exclusive use. As mentioned earlier in the chapter, this key is mirrored in HKEY_USERS\current user subkey\Software. This key is created the first time you call the SaveSetting function, as shown in the next section.

As application-specific ...

Get Access™ 2007 VBA Programmer's Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.