Multiple Apps

Save Settings to the Registry

Ease of Use

Intermediate

Version tested with

2000, 2002, 2003 

Submitted by:

mdmackillop

Description:

Demonstrates saving data to and retrieving data from the Windows registry, and deleting the Section and Key values. 

Discussion:

For storage of FilePath, FileName and other such data for use by generalised code. 

Code:

instructions for use

			

Option Explicit Private Sub UserForm_Initialize() TextBox1 = "Enter Path or other data sample" TextBox2 = "Enter File or other data sample" End Sub Private Sub CommandButton1_Click() SaveSetting "FindMyFile", "Fpath", "Data", TextBox1 SaveSetting "FindMyFile", "FName", "Data", TextBox2 End Sub Private Sub CommandButton2_Click() Label1.Caption = GetSetting(appname:="FindMyFile", Section:="FPath", Key:="Data") Label2.Caption = GetSetting(appname:="FindMyFile", Section:="FName", Key:="Data") End Sub Private Sub CommandButton3_Click() On Error GoTo Finish 'Error occurs if there are no values to delete. DeleteSetting "FindMyFile", "FPath" DeleteSetting "FindMyFile", "FName" 'Update labels CommandButton2_Click Finish: End Sub

How to use:

  1. Although the example is in Word, no changes are required to use this in Excel or other applications.
  2. Create a userform having two textboxes, three commandbuttons and two labels.
  3. To see how the data is stored, open the registry (Start-Run and type "regedit" and hit enter) and search for FindMyFile.
  4. Caution:
  5. Although the code makes changes to the Registry in a specific section "VB and VBA Program Setting", you should not manually edit the registry when it is accessed using the RegEdit. Improper changes to the registry can have unforeseen consequences.
 

Test the code:

  1. Click button 1 to write the information in the textboxes to the registry.
  2. Click button 2 to enter the data from the registry as label captions.
  3. Click button 3 to delete the setting and data from the registry and update the Label values.
 

Sample File:

SaveSettings.zip 16.13KB 

Approved by mdmackillop


This entry has been viewed 286 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express