You can Stop, Start, Pause, Resume, Disable, and Enable Windows Services with Microsoft Access VBA. Access VBA can do this by using the power of WMI (Windows Management Instrumentation). We are able to do this by using late binding to access the WbemScripting library. With late binding you do not have to worry about setting References or having broken references. But if you want to use early binding or use the Object Browser to learn more about using WMI you can set a reference to the Microsoft WMI Scripting V1.2 Library. The full name and path is C:\Windows\System32\wbem\wbemdisp.tlb on my computer.
In our last article we described Windows Services as the means by which Programs or Windows Processes to get things done as well as carry out vital functions for computer operations.
We recommend again that caution is used because changing Services settings can ruin a computer’s ability to function. It is vital to gain the knowledge necessary to safely manage Windows Services. We recommend a close reading of the Win32_Service Class webpage and related pages that document the Win32_Service Class Methods.
We want to show how you can use Access to stop and start Services which requires changing the “State” of the Service. But first we have know whether or not a Service is already running and if it can be started or stopped because you can’t start a Service if it is Disabled. You have to change the Start Mode first.
There are five Start Modes: Boot, System, Automatic, Manual, and Disabled. Using VBA and WMI provides even greater control than is provided by the Microsoft Management Console (MMC) Services Snap-in where your choices are usually just Automatic, Manual, or Disabled.
Microsoft provides the following information about the Start Mode of Services:
- Boot: Device driver started by the operating system loader. This value is valid only for driver services.
- System: Device driver started by the operating system initialization process. This value is valid only for driver services.
- Automatic: Service to be started automatically by the Service Control Manager during system startup.
- Manual: Service to be started by the Service Control Manager when a process calls the StartService method.
- Disabled: Service that can no longer be started.
As you can see from the list, it would be best if we do not change the Start Mode of most Services since changing from System to Boot, for example, may cause the Service to no longer work properly. No doubt that is why the MMC limits our Start Mode options. But there are some unwanted services we may want to to change to Disabled so they will not run at all. And there may be some services that we want to change from Disabled to Manual or Automatic so the Service will start as needed.
One of the benefits of using the Win32_Services Form is that we can gain information about all the services on our computer. It is very important to click the “Update Services Data” button to get the current information about a Service because old data may be incorrect. The Description is very helpful. We can see if the Service is Started, and the State informs us whether the service is running, stopped, or paused. We especially need to see the Start Mode, which we are preparing to set.

To enable changing the Start Mode of the Service in the Current Record we added a Combo box to the form. We have named our Combo box “cboSetStartMode”, set the Row Source Type to “Value List”, and added the following to the Row Source Property: Boot;System;Automatic;Manual;Disabled.
In the Combo box AfterUpdate Event we have this code:
Private Sub cboSetStartMode_AfterUpdate()
Dim strServiceName As String
Dim strSetStartMode As String
' Get the Name of the Service listed in this Record
strServiceName = Me.txtName.Value
' Get the Start Mode selected in the Combo box
strSetStartMode = Me.cboSetStartMode.Value
' Call the procedure to change the Start Mode
Call SetStartMode(strServiceName, strSetStartMode)
End Sub
This procedure calls the SetStartMode Sub. The critical code of the SetStartMode procedure is errReturnCode = objService.ChangeStartMode(strStartMode). This code attempts to change the Start Mode and returns an error code integer signifying Success with 0 or an Error with any other value.
Since the errReturnCode’s default value is 0, we set it to a value not used by Windows near the beginning of the procedure, using the number 9999. We do this to avoid a silent, but significant error. If we did not change this value from 0, we could easily get a false return that the Start Mode had been successfully changed, when in fact it had not been changed at all! This would happen if there was no instance of the Service found, which would cause the line of code errReturnCode = objService.ChangeStartMode(strStartMode), that changes the Start Mode and gets the error return value, to be skipped.
If the errReturnCode remains at 9999 a Message box appears to report the error. Other error codes are changed to the corresponding error text by calling the errReturnStartMode Function.
Sub SetStartMode(strServiceName As String, strStartMode As String)
Dim objWMIService As Object
Dim colServices As Object
Dim objService As Object
Dim strComputer As String
Dim strErrorMessage As String
Dim errReturnCode As Long
DoCmd.Hourglass True
' Set an error code not used by Microsoft
errReturnCode = 9999
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\" _
& strComputer & "\root\cimv2")
Set colServices = objWMIService.ExecQuery _
("SELECT * FROM win32_Service" & _
" WHERE Name = '" & strServiceName & "'")
For Each objService In colServices
' Use the ChangeStartMode Method to change the Start Mode
' errReturnCode informs us of success or errors
errReturnCode = objService.ChangeStartMode(strStartMode)
Next
DoCmd.Hourglass False
' Get the Success or Error message
Select Case errReturnCode
Case Is = 0
MsgBox "Service " & strServiceName & " has been set to " _
& strStartMode & " Start Mode successfully. " _
, vbInformation
Case Is = 9999
' If errReturnCode has not changed the Service was not found
MsgBox "Service """ & strServiceName & """ was not found" & _
" and may not exist. ", vbCritical
Case Else
' Get the error messages supplied by Microsoft
strErrorMessage = errReturnStartMode(errReturnCode)
MsgBox "Service " & strServiceName _
& " Start Mode change failed. " & vbCrLf _
& "Reason: " & strErrorMessage & " ", _
vbExclamation, "Service Start Mode Change Failed"
End Select
Set objService = Nothing
Set colServices = Nothing
Set objWMIService = Nothing
End Sub
When the errReturnCode is passed to this Function the error message provided by Microsoft is returned. We used data from Microsoft’s ChangeStartMode Method of the Win32_Service Class web page to build the Function.
Function errReturnStartMode(lngerrReturn As Long) As String
Select Case lngerrReturn
Case Is = 0
errReturnStartMode = "Success"
Case Is = 1
errReturnStartMode = "Not Supported"
Case Is = 2
errReturnStartMode = "Access Denied"
Case Is = 3
errReturnStartMode = "Dependent Services Running"
Case Is = 4
errReturnStartMode = "Invalid Service Control"
Case Is = 5
errReturnStartMode = "Service Cannot Accept Control"
Case Is = 6
errReturnStartMode = "Service Not Active"
Case Is = 7
errReturnStartMode = "Service Request Timeout"
Case Is = 8
errReturnStartMode = "Unknown Failure"
Case Is = 9
errReturnStartMode = "Path Not Found"
Case Is = 10
errReturnStartMode = "Service Already Running"
Case Is = 11
errReturnStartMode = "Service Database Locked"
Case Is = 12
errReturnStartMode = "Service Dependency Deleted"
Case Is = 13
errReturnStartMode = "Service Dependency Failure"
Case Is = 14
errReturnStartMode = "Service Disabled"
Case Is = 15
errReturnStartMode = "Service Logon Failure"
Case Is = 16
errReturnStartMode = "Service Marked For Deletion"
Case Is = 17
errReturnStartMode = "Service No Thread"
Case Is = 18
errReturnStartMode = "Status Circular Dependency"
Case Is = 19
errReturnStartMode = "Status Duplicate Name"
Case Is = 20
errReturnStartMode = "Status Invalid Name"
Case Is = 21
errReturnStartMode = "Status Invalid Parameter - Selected Start Mode is Invalid"
Case Is = 22
errReturnStartMode = "Status Invalid Service Account"
Case Is = 23
errReturnStartMode = "Status Service Exists"
Case Is = 24
errReturnStartMode = "Service Already Paused"
End Select
End Function
So to change the Start Mode of a Windows Service with our Form, we use the “Find Service” Combo box to move to the Service’s Record. Next we carefully note the information on the Form about the Service so we make a safe decision about what we plan to do. Then we use the “Set Start Mode” Combo box to change the Service Start Mode. The message box will then let us know whether or not the Start Mode was changed, and if not, the reason for the failure.
In our next article we plan to demonstrate starting, stopping, pausing, and resuming services.
Republished from Access Easy Tips [18 clicks].
Read the original version here [32134 clicks].