Avatar of AutomateMyOffice
AutomateMyOffice
 asked on

Edit registry in HKLM using Microsoft Access 2013 VBA?

I am trying to manipulate the registry from VBA code in Microsoft Access 2013 under Windows 7, 32-bit.  I don't have a problem in HKCU, but in HKLM I receive an error.

Dim ChangeReg
Set ChangeReg = CreateObject("WScript.Shell")
ChangeReg.RegWrite "HKLM\MyNewKey\", 1, "REG_DWORD"

...throws run-time error:  Invalid root in registry key "HKLM\MyNewKey\".

I am guessing this is security-related, and I would like to get around it without causing the user to need to click on a UAC dialog box or anything like that, if possible.  Suggestions?  Thanks.
Microsoft AccessVB ScriptVBA

Avatar of undefined
Last Comment
Luke Chung

8/22/2022 - Mon
Luke Chung

You need admin rights to modify the machine entries. If the user doesn't have those rights, your app won't be able to make changes to that part of the registry.

Our paper on how to handle temporary data may help:
http://www.fmsinc.com/MicrosoftAccess/database-design/temporary-data.html
AutomateMyOffice

ASKER
I am getting the error even though I am an administrator on my development computer.  The end users will also have admin rights.

This is not a matter of storing data.  It is a need to have programmatic control over certain functions that depend on values in the registry, such as AutoRotation on a Windows 8 tablet.
Surone1

try this:

Dim ChangeReg
Set ChangeReg = CreateObject("WScript.Shell")
ChangeReg.RegWrite "HKLM\MyNewKey", 1, "REG_DWORD"
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Luke Chung

Dealing with registry entries usually involves much more complicated Windows API calls to modify the registry. We have some of that code in our Total Visual SourceBook product (registry modules), but that may be overkill.

Are you supporting multiple users on the same device? If not, the built-in Access VBA commands GetSetting, SetSetting, DeleteSetting that I referenced earlier deals with the registry values without the complexity of using the Shell command.

Hope this helps.
AutomateMyOffice

ASKER
Surone1 -- Removing the trailing backslash changes the error to "Permisson denied," but i still can't create the key.

LukeChung -- I do not see where you earlier referenced GetSetting, etc.  (Spent a while googling SetSetting until I realized it is actually SaveSetting!)  These may help, and the SourceBook looks like a good resource as well.  

What I really need is:

a) to know whether it is possible for a user having admin rights to create, modify or delete registry values under HKLM (not HKCU) using Microsoft Access VBA, without the need to take any other special action, such as responding to a UAC dialog box,

b) a couple of lines of code, whether using SaveSetting, FMS modules, or anything else, showing how to accomplish the simple addition of a new key, as I was attempting to do with the snippet contained in my original question.

I appreciate any assistance that can be offered.
SOLUTION
Surone1

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.
ASKER CERTIFIED SOLUTION
Luke Chung

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
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.