SaveSetting Procedure |
Microsoft.VisualBasic.Interaction
SaveSetting( appname , section , key , setting )
The name of the application
The name of the registry key
The name of the value entry whose value is to be saved
The value to save
Creates or saves an entry for a VB application in the Windows registry
If either the appname or section subkeys are not found in the registry, they are automatically created.
The function writes a value to a subkey of the KEY_CURRENT_USER\Software\ VB and VBA Program Settings key of the registry.
section need not be an immediate subkey of appname ; instead, section can be a fully qualified path to a nested subkey, with each subkey separated from its parent by a backslash. For example, a value of Settings\Coordinates for the section argument indicates that the value is to be retrieved from HKEY_CURRENT_USER\Software\VB and VBA Program Settings\appname\Settings\Coordinates .
Visual Basic writes setting to the registry as a string ( REG_SZ ) value. If setting is not a string, VB attempts to coerce it into a string in order to write it.
If the setting cannot be saved, a runtime error will be generated.
The built-in registry-manipulation functions allow you to create professional 32-bit applications that use the registry for holding application-specific data, in the same way that .INI files were used in the 16-bit environment. You can, for example, store information about the user 's desktop settings (i.e., the size and position of forms) the last time the program was run.
Since it writes to the current user's registry key, SaveSetting should be used exclusively for storing user settings; it should not be used to store nonuser information (i.e., hardware information, system-level information, or application information that is independent of the user).
GetSetting , GetAllSettings , and SaveSetting allow you direct access to only a limited section of the Windows registry, that being a special branch created for your application ( HKEY_CURRENT_USER\Software\VB and VBA Program Settings\ yourappname ) .
SaveSetting does not allow you to write to the default value of a registry key. Attempting to do so produces runtime error 5, "Invalid procedure call or argument." This is not as great a limitation as it may appear, since GetSetting also cannot retrieve a default value from a registry key.
This may seem obvious, but it has been often overlooked: if a user hasn't run the application before and your application's initialization doesn't set up the registry structure for the application, the key values won't be there.
The previous point is particularly applicable when running your application on Windows in a multiuser environment since Microsoft chose to use the HKEY_CURRENT_USER branch of the registry to store entries for VB applications. This means that your application can be running swimmingly for one user, but when another user logs onto the machine, the registry settings are not available.
Rather than rely on the relatively underpowered registry-access functionality available in Visual Basic, we highly recommend that you instead use the Registry and RegistryKey classes available in the BCL's Microsoft.Win32 namespace.
DeleteSetting Procedure, GetAllSettings Function, GetSetting Function