Our last two articles were about using WMI to manage Processes which are software programs that run on our computers. Processes are like CEOs that are up front and visible. Windows Services are like the CEO’s Secretary who is the one who really gets things done in the background. Some Processes are also Services, but most Services start when the computer boots or when a Program is opened or needs to perform a particular action such as Printing.
Managing Services can be useful to the Access Developer. You may need to pause, stop, disable, enable, or start a Service before performing some action with Access. You may need to manage some SQL Server Services or Network Services.
As I was working to build a table and form to manage Windows Services I found a browser toolbar that I knew nothing about. Other examples of Services are Software Updaters, SQL Server Services, Network Services, some Drivers, Logging and Reporting Services, and much more. In fact, Services enable most everything done by a Computer.
This brings us to an important point. When you deal with Services, you better know what you are doing or you can cause yourself a lot of grief. In future articles we will Stop, Start, Pause, Resume, Disable, and Enable some Services. We could easily demonstrate how to Delete Services, but I don’t want to help some poor soul accidentally cripple their Computer.
In this article we will show you how to get Services data, append it to a table, and display it in a Form. In upcoming articles we will use this Form to manage Services.

We have a command button on our Form that is used to get the Services data. Because Services information changes often, we don’t feel a need to keep past Services data so we delete all the existing records in our table and then fill it with the latest data.
Private Sub cmdGetServicesData_Click()
On Error GoTo Err_cmdGetServicesData_Click
Dim db As DAO.Database
Dim strSQL As String
strSQL = "DELETE * FROM tblWin32_Service"
Set db = CurrentDb
' Delete all records in the Table
' In case of error, no records are changed
db.Execute strSQL, dbFailOnError
Set db = Nothing
' The following code takes a while to run
' Let the user know Access is working
DoCmd.Hourglass True
' Fill the Table with the current Service Data
Call FilltblWin32_Services("tblWin32_Service", "Win32_Service")
' Fill the form with the new data
Me.Requery
' Update the Find Service Combo box
Me.cboFindService.Requery
Exit_cmdGetServicesData_Click:
' Make sure the Hourglass is Off
' This code will run even if there is an error
DoCmd.Hourglass False
Exit Sub
Err_cmdGetServicesData_Click:
MsgBox "Error " & Err.Number & " (" & Err.Description & ")" & _
" In procedure cmdGetServicesData_Click"
Resume Exit_cmdGetServicesData_Click
End Sub
The cmdGetServicesData_Click sub calls the FilltblWin32_Services Procedure that fills the table with data.
Sub FilltblWin32_Services(strTableName As String, strClass As String)
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim objWMIService As Object
Dim colInstances As Object
Dim objInstance As Object
Dim colProperties As Object
Dim objProperty As Object
Dim strComputer As String
Dim strNameSpace As String
Dim strPropName As String
Dim strPropValue As String
Dim varID
strComputer = "."
strNameSpace = "\root\cimv2"
Set objWMIService = GetObject("winmgmts:" & _
"{impersonationLevel=impersonate}!\" & _
strComputer & strNameSpace)
Set colInstances = objWMIService.ExecQuery( _
"SELECT * FROM " & strClass, , 48)
'Set a pointer to the Database
Set db = CurrentDb
'Instantiate the recordset
Set rst = db.OpenRecordset(strTableName, dbOpenDynaset)
' Get Each Instance
For Each objInstance In colInstances
'Add a New Record to the Recordset
rst.AddNew
' Get the Property Names and Values in each Instance
Set colProperties = objInstance.Properties_
For Each objProperty In colProperties
strPropName = objProperty.Name
' Avoid errors caused by Nulls
strPropValue = CStr(Nz(objProperty.Value, vbNullString))
' Change Zero Length Strings to the string "Null"
If Len(strPropValue & vbNullString) = 0 Then
strPropValue = "Null"
Else
' Some Values need to be converted to be understood
Select Case strPropName
Case "ErrorControl"
strPropValue = GetErrorControl(strPropValue)
Case "StartMode"
strPropValue = GetStartMode(strPropValue)
Case "InstallDate"
strPropValue = WMIDateConvert(strPropValue)
End Select
End If
' Add the value to the Recordset
rst(strPropName) = strPropValue
Next objProperty
' This table does not use Autonumber so
' get the previous high number and add 1
varID = DMax("ServiceID", "tblWin32_Service") + 1
' If this is first one, then value will be null
If IsNull(varID) Then varID = 1
rst!ServiceID = varID
' Get the current Date and Time
rst!LastUpdated = Now()
' Do not forget to save the new Record to the Recordset
rst.Update
Next objInstance
' Release Memory
rst.Close
Set objProperty = Nothing
Set colProperties = Nothing
Set objInstance = Nothing
Set colInstances = Nothing
Set objWMIService = Nothing
Set rst = Nothing
Set db = Nothing
End Sub
After the table is filled with the data, the code returns to our initial sub and requeries the Form so it will display the Services information.
You may find some Services there you do not want or need and you may find some that are not running, but you want to start. We plan to write about how to manage Services in our next article.
Republished from Access Easy Tips [18 clicks].
Read the original version here [32134 clicks].