Saving to and retrieving from the registry

Maggi

New Member
Joined
Jun 19, 2003
Messages
30
The following code is supposed to save status of option buttons and check boxes in the current sheet into the registry on the click of a button:
Code:
Sub CommandButton2_Click()
    Dim ctrl As OLEObject
    Dim CtrlType As String
    For Each ctrl In Me.OLEObjects
        CtrlType = TypeName(ctrl)
        If CtrlType = "CheckBox" Or CtrlType = "OptionButton" Then
            SaveSetting APPNAME, "Defaults", ctrl.Name, ctrl.Value
        End If
    Next ctrl
    MsgBox "Current settings have been saved as default!"
End Sub
And then whenever the file is opened, the following code is supposed to retrieve the saved defaults from the registry:
Code:
Private Sub Workbook_Open()
    Dim ctrl As OLEObject
    Dim CtrlType As String
    For Each ctrl In Me.OLEObjects   
        CtrlType = TypeName(ctrl)       
        If CtrlType = "CheckBox" Or CtrlType = "OptionButton" Then
            ctrl.Value = GetSetting(APPNAME, "Defaults", ctrl.Name, ctrl.Value)
        End If
    Next ctl
End Sub
But, as usually ( :unsure: ) it doesn't work, although there are no error messages.
Any ideas would be much appreciated...
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
I expect your problem is the use of the Me keyword in your Workbook_Open event procedure. In that context Me is the Workbook not the Worksheet containing your controls. So you need something like:

For Each ctrl In Me.Worksheets("Sheet1").OLEObjects
 
Upvote 0
Ok! But for some reason the defaults don't get saved in the registry in the first place.
 
Upvote 0
Is SaveSetting working ? I mean, do you see the entry under VB & VBA Program Settings ?

If that works, the problem that I see with GetSetting is that it returns a string, "True" or "False" which are NOT the boolean values that the checkbox would expect...
 
Upvote 0
Andrew Poulsom said:
What's APPNAME?
AFAIK, it's a global constant that holds the application name.


Juan Pablo González said:
Is SaveSetting working ? I mean, do you see the entry under VB & VBA Program Settings ?
Nope, that's the problem (for now), can't find it in the registry...
 
Upvote 0
APPNAME needs to be defined somewhere. It's not "builtin", like you could use in VB

App.Title

APPNAME should be in a standard module like

Public Const APPNAME As String = "My Title"

And maybe its not saving anything in the registry because, as pointed by Andre, its not going inside the loop ?
 
Upvote 0

Forum statistics

Threads
1,213,534
Messages
6,114,184
Members
448,554
Latest member
Gleisner2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top