+ Reply to Thread
Results 1 to 10 of 10

Using the Registry. How to know if Key exists?

  1. #1
    Forum Contributor Rick_Stanich's Avatar
    Join Date
    11-21-2005
    Location
    Ladson SC
    MS-Off Ver
    Office365
    Posts
    1,177

    Using the Registry. How to know if Key exists?

    Cross posted at Access World.
    http://www.access-programmers.co.uk/...d.php?t=182552

    This is a sample structure.
    HKEY_CURRENT_USER\Software\VB and VBA Program Settings\GeoMeasure\CMM Data\SomeKey
    The text in orange is the Section Name, the text in violet is the Key.
    Writing to and reading from the registry is easy to do, determining if a "Key" exists is not so easy.
    In this case the string "HKEY_CURRENT_USER\Software\VB and VBA Program Settings" will always be the same, its the standard naming thru VBA.

    How can I find out if a specific Key exists in "HKEY_CURRENT_USER\Software\VB and VBA Program Settings"?

    FYI
    There is an example for "IsBroken" but it appears to be for file association only.
    Last edited by Rick_Stanich; 11-04-2009 at 11:46 AM.
    Regards

    Rick
    Win10, Office 365

  2. #2
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,978

    Re: Using the Registry. How to know if Key exists?

    You can use GetAllSettings:
    Please Login or Register  to view this content.
    Everyone who confuses correlation and causation ends up dead.

  3. #3
    Forum Contributor Rick_Stanich's Avatar
    Join Date
    11-21-2005
    Location
    Ladson SC
    MS-Off Ver
    Office365
    Posts
    1,177

    Re: Using the Registry. How to know if Key exists?

    Thank you, your example works for "Section Name", not Keys which means I did not ask my question correctly.

    I have fixed my original question as follows:
    This is a sample structure.
    HKEY_CURRENT_USER\Software\VB and VBA Program Settings\GeoMeasure\CMM Data\SomeKey\
    The text in orange is the SectionName, the text in violet is the Key.
    Writing to and reading from the registry is easy to do, determining if a "Key" exists is not so easy.
    In this case the string "HKEY_CURRENT_USER\Software\VB and VBA Program Settings" will always be the same, its the standard naming thru VBA.

    How can I find out if a specific Key exists in "HKEY_CURRENT_USER\Software\VB and VBA Program Settings"?

  4. #4
    Valued Forum Contributor
    Join Date
    07-21-2008
    Location
    London, UK
    Posts
    326

    Re: Using the Registry. How to know if Key exists?

    I cant test this at work so please forgive me -- might help
    Please Login or Register  to view this content.

  5. #5
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,978

    Re: Using the Registry. How to know if Key exists?

    You can still use the code above. If the App Name and section exist, then the returned value is an array of keys and values, so you just need to loop through it.

  6. #6
    Forum Contributor Rick_Stanich's Avatar
    Join Date
    11-21-2005
    Location
    Ladson SC
    MS-Off Ver
    Office365
    Posts
    1,177

    Re: Using the Registry. How to know if Key exists?

    A simple change to romperstomper's example worked out.
    Please Login or Register  to view this content.
    Strange how "IsEmpty(varSettings)" did not work.
    Using:
    Please Login or Register  to view this content.
    Displays an empty messagebox when I use a Key that doesnt exist.
    Last edited by Rick_Stanich; 11-04-2009 at 11:53 AM.

  7. #7
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Using the Registry. How to know if Key exists?

    Strange how "IsEmpty(varSettings)" did not work.
    From Help:
    GetSetting(appname, section, key[, default])

    The GetSetting function syntax has these named arguments:

    appname Required. String expression containing the name of the application or project whose key setting is requested. On the Macintosh, this is the filename of the initialization file in the Preferences folder in the System folder.
    section Required. String expression containing the name of the section where the key setting is found.
    key Required. String expression containing the name of the key setting to return.
    default Optional. Expression containing the value to return if no value is set in the key setting. If omitted, default is assumed to be a zero-length string ("").
    Entia non sunt multiplicanda sine necessitate

  8. #8
    Forum Contributor Rick_Stanich's Avatar
    Join Date
    11-21-2005
    Location
    Ladson SC
    MS-Off Ver
    Office365
    Posts
    1,177

    Re: Using the Registry. How to know if Key exists?

    I did read that in my help file, and I am failing to understand why IsEmpty did not work.
    I guess I am not understanding how to use IsEmpty.

  9. #9
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,978

    Re: Using the Registry. How to know if Key exists?

    IsEmpty fails because the function returns "", and so the Variant actually contains an empty string, not nothing.

  10. #10
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Using the Registry. How to know if Key exists?

    Also, Empty <> IsEmpty(). Run this and look at the results in the Immediate window:

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    Last edited by shg; 11-04-2009 at 01:24 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1