SaveSetting Statement

Named Arguments

Yes

Syntax

SaveSetting appname, section, key, setting


appname

Use: Required

Data Type: String

The name of the application.


section

Use: Required

Data Type: String

The name of the registry key.


key

Use: Required

Data Type: String

The name of the value entry whose value is to be saved.


setting

Use: Required

Data Type: String or numeric

The value to save.

Description

Creates or saves an entry for a VB application in the Windows registry.

Rules at a Glance

  • If either the appname or section subkeys isn't found in the registry, it's automatically created.

  • The function writes a value to a subkey of the KEY_CURRENT_USERSoftwareVB 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 SettingsCoordinates for the section argument indicates that the value is to be retrieved from HKEY_CURRENT_USERSoftwareVB and VBA Program Settings appnameSettingsCoordinates.

  • Visual Basic writes setting to the registry as a string (REG_SZ) value. If setting isn't a string, VB attempts to coerce it into a string in order to write it.

  • If the setting can't be saved, a runtime error is generated.

Programming Tips and Gotchas

  • Use the App object's EXEName property to pass your application's name to the GetSetting function.

  • 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 .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, for example) 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 shouldn't 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_USERSoftwareVB and VBA Program Settingsyourappname). You can't access or change other registry settings without resorting to the Windows API.

  • SaveSetting doesn't 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 isn't as great a limitation as it may appear, since GetSetting also can't retrieve a default value from a registry key.

  • It may seem obvious but 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 above point is particularly apt when running your application on Windows NT and Windows 95 in a multiuser environment, since Microsoft chose to use the HKEY_CURRENT_USER branch of the registry to store entries for VB and VBA applications. This means that your application can be running swimmingly for one user, but when another user logs onto the machine, the registry settings aren't available.

See Also

DeleteSetting Statement, GetSetting Function, GetAllSettings Function
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset