Saving to and retrieving from the registry

Thanks Thanks:  0
Likes Likes:  0
Results 1 to 8 of 8

Thread: Saving to and retrieving from the registry

  1. #1
    New Member
    Join Date
    Jun 2003
    Posts
    30
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Saving to and retrieving from the registry

     
    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 ( ) it doesn't work, although there are no error messages.
    Any ideas would be much appreciated...

  2. #2
    MrExcel MVP
    Moderator
    Andrew Poulsom's Avatar
    Join Date
    Jul 2002
    Posts
    73,092
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Saving to and retrieving from the registry

    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

  3. #3
    New Member
    Join Date
    Jun 2003
    Posts
    30
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Saving to and retrieving from the registry

    Ok! But for some reason the defaults don't get saved in the registry in the first place.

  4. #4
    MrExcel MVP
    Moderator
    Andrew Poulsom's Avatar
    Join Date
    Jul 2002
    Posts
    73,092
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Saving to and retrieving from the registry

    What's APPNAME?

  5. #5
    MrExcel MVP
    Join Date
    Feb 2002
    Location
    Bogota, Colombia
    Posts
    11,959
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Saving to and retrieving from the registry

    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...
    Regards,

    Juan Pablo González
    http://www.juanpg.com

  6. #6
    New Member
    Join Date
    Jun 2003
    Posts
    30
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Saving to and retrieving from the registry

    Quote Originally Posted by Andrew Poulsom
    What's APPNAME?
    AFAIK, it's a global constant that holds the application name.


    Quote Originally Posted by Juan Pablo González
    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...

  7. #7
    MrExcel MVP
    Join Date
    Feb 2002
    Location
    Bogota, Colombia
    Posts
    11,959
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Saving to and retrieving from the registry

    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 ?
    Regards,

    Juan Pablo González
    http://www.juanpg.com

  8. #8
    MrExcel MVP
    Moderator
    Andrew Poulsom's Avatar
    Join Date
    Jul 2002
    Posts
    73,092
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Saving to and retrieving from the registry

      
    [quote="Maggi"]AFAIK, it's a global constant that holds the application name.

    What do you get if you type?

    MsgBox APPNAME

    in the Immediate window?

User Tag List

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

 

 
DMCA.com