VBA code to check if Access is run as an administrator

isladogs

MVP / VIP
Local time
Today, 13:53
Joined
Jan 14, 2017
Messages
18,823
Does anyone know how to detect using VBA whether Access has been opened as an administrator?

The reason for asking is that I need to write to the HKEY_LOCAL_MACHINE hive of the registry in one of my production databases.
Writing the code to do this isn't an issue for me

The issue is that changes to HKLM are not normally possible from within Access for security reasons.
This will cause the code to 'fail' - no error arises but no changes are made to the registry

However, this CAN be done in one of 3 ways:
1. Modify the registry so that standard users can edit the relevant key (Full Control) ... then open Access normally
2. Open Access as an administrator then run the code
3. Create a .reg file with the required change then run this outside Access

All of these work.
However, option 1 does open up the registry to potential misuse
My preference is for option 2 or 3 depending on how Access has been opened.

The idea is that if VBA detects run as admin = true then option 2 runs.
If false, option 3 is used.
 
Well, have you tested whether "Run As Admin" makes Environ("Username") come back as Admin?
 
Well, have you tested whether "Run As Admin" makes Environ("Username") come back as Admin?

Ah, that would be too easy ...
No it doesn't
 
I found the answer ....

Add this declaration to a module

Code:
Public Declare Function IsUserAnAdmin Lib "shell32" () As Long

IsUserAnAdmin returns 0 if Access opened normally
If Access run as Administrator, it returns 1

Perfect!

NOTE: Checked in both A2010 & A2016
 
I found the answer ....

Add this declaration to a module

Code:
Public Declare Function IsUserAnAdmin Lib "shell32" () As Long

IsUserAnAdmin returns 0 if Access opened normally
If Access run as Administrator, it returns 1

Perfect!

NOTE: Checked in both A2010 & A2016
What a perfect answer. Worked perfectly for me. Thanks!!
 
I'd forgotten all about this thread.
Since then, I have used that code in many of my apps and it always works perfectly

Anyway, glad its of use to someone else almost 6 years on ... 😎
 

Users who are viewing this thread

Back
Top Bottom