Solved

Adding a registry key using VBA.

Posted on 2014-01-09
4
2,581 Views
Last Modified: 2014-01-09
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.
0
Comment
Question by:dsoderstrom
  • 2
4 Comments
 
LVL 13

Expert Comment

by:Alexander Eßer [Alex140181]
ID: 39768423
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

0
 
LVL 119

Accepted Solution

by:
Rey Obrero earned 500 total points
ID: 39768440
try this

If RegKeyExists("HKEY_CURRENT_USER\Software\Microsoft\Office\14.0\Access\Security\Trusted Locations\BEApplication\") = False Then
        ' Code for adding registry key
            Dim myWS As Object, strPath
            strPath = <Folder Path>
            Set myWS = CreateObject("WScript.Shell")
            
            myWS.RegWrite "HKEY_CURRENT_USER\Software\Microsoft\Office\14.0\Access\Security\Trusted Locations\BEApplication\Path",strPath,"REG_SZ"

            
    Else
        ' Key exists - do nothing
    End If
0
 

Author Closing Comment

by:dsoderstrom
ID: 39769196
Worked perfectly!  Thank You
0
 
LVL 13

Expert Comment

by:Alexander Eßer [Alex140181]
ID: 39769504
nice, very nice.... speechless
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now