VBA sub run as administrator (1 Viewer)

ukaquanaut

Registered User.
Local time
Yesterday, 16:52
Joined
Feb 8, 2012
Messages
33
Hi
I used Access to develop a 2D label printing App connected to my clients ERP system. We use Bartender Label Printing to automate the Barcode printing and this all, in general works really well, over 3 millions labels printed from in WW.

This requires Bartender automation system to be ready to go and occasionally its not the background services stop and printing halts. Some of the users have got used to restarting the Bartender background process using the Bartender Utilities, but this is not a realistic process for some users to go of the Access App and use the other utilities in Windows.

Therefore I have added a monitor page to the Access App to indicate when a Bartender Process is not running, this works perfectly and the associated button to start the Windows Bartender Process also works as long as you run the Access as Administrator. Alas that's not really practical as certainly the Runtime Access appears not to respond that same way in this regard as the developer version of Access.

'---------- START Administrator -------------------------------------
Dim svc As Variant
Dim service As String

service = "BarTender Integration Service"

If Me.CmdP1Action.Caption = "Start" Then

MsgBox "INFO: Lets start process: " & service

For Each svc In GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2").ExecQuery("SELECT * FROM Win32_Service WHERE Name='" & service & "'")
svc.StartService
Exit For
Next
ElseIf Me.CmdP1Action.Caption = "Restart" Then
MsgBox "INFO: Lets Restart process: " & service
For Each svc In GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2").ExecQuery("SELECT * FROM Win32_Service WHERE Name='" & service & "'")
svc.StopService
Exit For
Next
Sleep (10000)
For Each svc In GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2").ExecQuery("SELECT * FROM Win32_Service WHERE Name='" & service & "'")
svc.StartService
Exit For
Next
End If
' ------------- END Administrator ----------------------------------

This is the code in a subroutine that I want to run with raised credentials as Administrator.

Does anyone know how to do this? I have seen other apps popup a box to get acknowledgement to raise credentials , but I have no idea how to get Access to do this

Many Thanks

Jerry Barrett
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 16:52
Joined
Oct 29, 2018
Messages
21,467
Hi. Just a thought, how about using PowerShell or VBScript instead of VBA?
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Yesterday, 18:52
Joined
Feb 28, 2001
Messages
27,172
In general, Windows is designed to not allow you to upgrade your process to change from running as one user to running as another user (in particular when the "other" user is SYSTEM or an Admin user.) A quick search of my Win32API books and some online searches tell me that what you want isn't really possible. (OK, at least CERTAINLY not easy.) Part of this is because the task's security descriptor is assigned at task creation time and lot of what you can do is derived from that descriptor. In order to change that descriptor, you have to be an admin anyway because it is in a protected area of memory that is not within the grasp of a non-admin user. I believe it is in the Kernel "Scratchpad" area or is associated with the process list (also in the Kernel scratchpad). Which is flat out NOT accessible to Joe Average User.

But there is another approach. In terms of looking at it from your end, you can't get there from here. BUT if you turn it around and look at it from another viewpoint, there might be a way to do this.

You have to more carefully write the code when doing this, but there is a way to set up an icon to run some app as an administrator. I.e. it ALWAYS runs as an Admin - but just because it runs as Admin doesn't mean that everything you do has to be an admin function. If you take the approach of launching the app as admin but NEVER let the users see anything other than data forms and control forms and other things that you can control, and if you then perform lockdown steps to make the other DB functions unavailable to the user, you might make some headway.
 
Last edited:

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Yesterday, 18:52
Joined
Feb 28, 2001
Messages
27,172
For an intermittent task, using the Task Scheduler with Run As options is pretty straight-forward, but I didn't get that vibe from the description. On the other hand, that is of course subjective.
 

isladogs

MVP / VIP
Local time
Today, 00:52
Joined
Jan 14, 2017
Messages
18,216
As suggested by @The_Doc_Man you could ensure that the app is run as an administrator but that only the specific task(s) then check this is the case before running.
I have some very simple code to check that. Let me know if you want me to upload it.

Having said that, I agree with the suggestions already made to run these specific tasks separately using either task scheduler or a script
 

ukaquanaut

Registered User.
Local time
Yesterday, 16:52
Joined
Feb 8, 2012
Messages
33
Hi Guys

Thank you for your comments and suggestions.

I'm not really clear on how I want to handle this, I thought about using scripts, but there's the added issue of managing the deployment environment and making sure every install is setup correctly, I think the scheduler idea falls into the same category.

I shall continue to investigate this. Thanks Again
Jerry
 

Users who are viewing this thread

Top Bottom