Save And Load Data From The Registery

'Add 2 Command Buttons and 3 Text Boxes to your form.
'At run-time, Add text to the Text Boxes. Press the second button to save the
'3 strings to the registery. Now delete the text from the Text Boxes and press
'the first button to load the strings back from the registery.
'Insert the following code to your form :

'Change 'My Company' to your company name and 'My Prog' to your program name.
Const ThisApp = "My Company"
Const ThisKey = "My Prog"
'The Strings will be saved under the key:
'HKEY_CURRENT_USER\Software\VB and VBA Program Settings\My Company\My Prog

Private Sub Command1_Click()
Dim Name, Phone, PostCode As String
'If the key is not exist, it will return 'Empty'
Name = GetSetting(ThisApp, ThisKey, "Name", "Empty")
Phone = GetSetting(ThisApp, ThisKey, "Phone", "Empty")
PostCode = GetSetting(ThisApp, ThisKey, "PostCode", "Empty")
Text1.Text = (Name)
Text2.Text = (Phone)
Text3.Text = (PostCode)
End Sub

Private Sub Command2_Click()
Dim Name, Phone, PostCode As String
'Text1 will be saved under HKEY_CURRENT_USER\...\My Prog\Name
SaveSetting ThisApp, ThisKey, "Name", Text1.Text
SaveSetting ThisApp, ThisKey, "Phone", Text2.Text
SaveSetting ThisApp, ThisKey, "PostCode", Text3.Text
End Sub

Private Sub Form_Load()
Command1.Caption = "load"
Command2.Caption = "save"
End Sub

Go Back