Avatar of dsoderstrom
dsoderstrom
 asked on

Adding a registry key using VBA.

I would like to add a trusted location key to the registry using vba.
I am using the following vba code to determine if the registry key already exists:

If RegKeyExists("HKEY_CURRENT_USER\Software\Microsoft\Office\14.0\Access\Security\Trusted Locations\BEApplication\") = False Then
        ' Code for adding registry key
    Else
        ' Key exists - do nothing
    End If

Function RegKeyExists(i_RegKey As String) As Boolean
Dim myWS As Object

  On Error GoTo ErrorHandler
  'access Windows scripting
  Set myWS = CreateObject("WScript.Shell")
  'try to read the registry key
  myWS.RegRead i_RegKey
  'key was found
  RegKeyExists = True
  Exit Function
 
ErrorHandler:
  'key was not found
  RegKeyExists = False
End Function

The above code works, returning True if the registry key exists and False if it does not.

I have created a .reg file for adding the registry key.   The contents of the .reg file are as follows:

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Microsoft\Office\14.0\Access\Security\Trusted Locations\BEApplication]
"Path"="C:\\Access2010Applications\\"
"Description"="Access2010Applications"

I can click on this .reg file and the registry key gets added successfully.
How do I accomplish the same thing via vba.
Microsoft Access

Avatar of undefined
Last Comment
Alex [***Alex140181***]

8/22/2022 - Mon
Alex [***Alex140181***]

I think this guide will help you:
http://slipstick.com/developer/read-and-change-a-registry-key-using-vba/

see function "RegKeySave":
Sub RegKeySave(i_RegKey As String, _
               i_Value As String, _
      Optional i_Type As String = "REG_DWORD")
Dim myWS As Object
 
  'access Windows scripting
  Set myWS = CreateObject("WScript.Shell")
  'write registry key
  myWS.RegWrite i_RegKey, i_Value, i_Type
 
End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Rey Obrero (Capricorn1)

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
dsoderstrom

ASKER
Worked perfectly!  Thank You
Alex [***Alex140181***]

nice, very nice.... speechless
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck