Using the Registry to Persist Data
The Registry is a great place to save your application's options, and user settings. And the great thing is that VB provides 4 easy functions to access it. This is the first in two article explaining how to use the Windows Registry.
Using SaveSetting and GetSetting
This function saves some data into the Registry database.
SaveSetting appname, section, key, setting
Before you start saving data into the Registry, you must decide on an appname which will be used throughout the application. The registry can be thought of as a computer. The appname is the equivalent of a drive, for the sake of these functions, although we will see later that this is not entirely the case. This must be a string, although it can be a number converted by the CStr function. Generally, the appname should remain constant throughout your program.
The section is the 'directory' of this model. Just like directories on a computer, it can contain a "" to signify subdirectories. To save the key in the 'root' section, pass vbNullChar for this parameter.
The key is the 'file' of this model
This is the information in the file. It can be a number or a string.
This function retrieves data from the registry database.
GetSetting(appname, section, key[, default])
appname, section and key are all the same as above
If Visual Basic cannot find the specified key, it will return the contents of default instead. This is optional, but useful for error checking in case the settings have not been saved before. If this is not included, and there is no data saved, the function will return a zero length string.
SaveSetting "Logon Master", "Users\User0", _ "Name", "John" ' This has saved "Logon Master\Users\User0\Name" ' with the data "John" Debug.Print GetSetting("Logon Master", _ "Users\User0", "Name", "Anonymous" ' This prints "John" Debug.Print GetSetting("Logon Master", _ "Users\User1", "Name", "Anonymous" ' However, this would print "Anonymous" ' because there is no data saved in ' "Logon Master\Users\User1\Name"
Page 1 of 2