Why do I receive a compile error message at Dim c as New Registry?

When executing the following code, I get a compile error message at Dim c as New Registry.  How do I avoid the error?

Dim c As New Registry
    With c
        .ClassKey = HKEY_CURRENT_USER
        .SectionKey = "Software\Microsoft\Office\12.0\Access\Security\Trusted Locations\RoboForm Data"
        .ValueKey = "Path"
        .ValueType = REG_DWORD
        .Value = "C:\Users\Tom\Documents\RoboForm Data"
    End With

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
What is the error message?
What is the error that you see?
Try removing the 'new' keyword
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

twarrenbAuthor Commented:
The compile error message is ..."User-defined type not defined."

Changing "Dim c as New Registry" to "Dim c as Registry" has not effect.
käµfm³d 👽Commented:
You receive that message because VBA does not know about the type "Registry". Did you download this code from somewhere? If so, please check that site for mention of any referenced DLL files they may have been using. If you cannot find which library contains this type, then you may have to code some Win API calls to access the registry.
käµfm³d 👽Commented:

Removing "new" has nothing to do with your error. All that will do is cause you to receive a "null-reference exception" (because "c" will not have value) when you do get the referenced DLL added.
twarrenbAuthor Commented:
Thanks kaufmed.

Yes, I downloaded the code.  But the site makes no reference to DLL files that they might have been using.  Coding Win API calls to access the registry is beyond my skill level.

käµfm³d 👽Commented:
Do you have the site handy?
twarrenbAuthor Commented:

I copied the code from a comment somewhere on this site.  I'm trying to locate it.

käµfm³d 👽Commented:
I have an inkling that this code *might* have been from a .NET-related question. If you can't find a better answer, I've trimmed down the code found at http://support.microsoft.com/kb/145679 to produce the following. It will do what you are attempting above, albeit with slightly more code. But it boils down to a function call, so you can reuse it throughout your code  :)

You specified DWORD above, but I think you might want a REG_SZ. My code reflects this, but if you truly want a DWORD, then switch the type you pass to the function call.
Option Compare Database

Public Const HKEY_CURRENT_USER = &H80000001

Public Const REG_SZ As Long = 1
Public Const REG_DWORD As Long = 4

Public Const KEY_SET_VALUE = &H2

Declare Function RegSetValueExString Lib "advapi32.dll" Alias _
   "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, _
   ByVal Reserved As Long, ByVal dwType As Long, ByVal lpValue As _
   String, ByVal cbData As Long) As Long
Declare Function RegCloseKey Lib "advapi32.dll" _
   (ByVal hKey As Long) As Long

Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias _
   "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, _
   ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As _
   Long) As Long

Public Sub SetKeyValue(key As String, valueName As String, valueSetting As Variant, valueType As Long)
    Dim hKey As Long
    RegOpenKeyEx HKEY_CURRENT_USER, key, 0, KEY_SET_VALUE, hKey
    RegSetValueExString hKey, valueName, 0&, valueType, valueSetting, Len(setting)
    RegCloseKey hKey
End Sub

Public Sub Test()
    Dim key As String
    key = "Software\Microsoft\Office\12.0\Access\Security\Trusted Locations\RoboForm Data"
    SetKeyValue key, "Path", "C:\Users\Tom\Documents\RoboForm Data", REG_SZ
End Sub

Open in new window

twarrenbAuthor Commented:

I greatly appreciate all the work you've put into this.  However, it doesn't do the job.
I'm using Access 2010.  So, I set the key to ...\Office\14.0\... to no avail, & back to ...\Office\12.0\...
Again, the code ran, but the selected folder does not appear in the Trusted Locations.

käµfm³d 👽Commented:
Can you confirm whether the key showed up in the registry?
twarrenbAuthor Commented:
No, the key does not appear in the registry.
twarrenbAuthor Commented:
Thanks a lot for your help.  I couldn't get the code to work.  I found another solution at ...


Const HKEY_CLASSES_ROOT = &H80000000
Const HKEY_CURRENT_USER = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
Const HKEY_USERS = &H80000003
Const HKEY_CURRENT_CONFIG = &H80000005
strProgram = "Excel"
strFolder = "c:\files"
strDescription = "my custom folder"
blnAllowSubFolders = True
blnAllowNetworkLocations = True
strParentKey = "Software\Microsoft\Office\12.0\" & strProgram & "\Security\Trusted Locations"
intHighest = 0
Set objRegistry = GetObject("winmgmts:\\.\root\default:StdRegProv")
objRegistry.EnumKey HKEY_CURRENT_USER, strParentKey, arrChildKeys
For Each strChildKey in arrChildKeys
    If CInt(Mid(strChildKey, 9)) > intHighest Then
        intHighest = CInt(Mid(strChildKey, 9))
    End If
strNewKey = strParentKey & "\Location" & CStr(intHighest + 1)
objRegistry.CreateKey HKEY_CURRENT_USER, strNewKey
objRegistry.SetStringValue HKEY_CURRENT_USER, strNewKey, "Path", strFolder
objRegistry.SetStringValue HKEY_CURRENT_USER, strNewKey, "Description", strDescription
If blnAllowSubFolders Then
    objRegistry.SetDWORDValue HKEY_CURRENT_USER, strNewKey, "AllowSubFolders", 1
End If
If blnAllowNetworkLocations Then
    objRegistry.SetDWORDValue HKEY_CURRENT_USER, strParentKey, "AllowNetworkLocations", 1

End If
Question PAQ'd, 250 points refunded, and stored in the solution database.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.