UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome to UtterAccess! Please ( Login   or   Register )

Custom Search
 
   Reply to this topicStart new topic
> List Box In An Access For To Be Populated By VBA With Variables    
 
   
cdaigle1975
post Jan 2 2014, 02:16 PM
Post#1



Posts: 1
Joined: 2-January 14



Quite frankly I am banging my head into my desk.
Here is my code.
Private Sub Command22_Click()
strComputer = PC_NAME
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Service", , 48)
For Each objItem In colItems
Me.List45.AddItem objItem.name & ";" & objItem.State
Next
End Sub
What i want to achieve is to get the running processes from a remote machine and put them into a list box.
PC_NAME is grabbed from a text field on the form and List45 is the name of my unbound list box in the same form.
and yes i know i can get rid of the strComputer variable.
Go to the top of the page
 
DanielPineault
post Jan 2 2014, 02:51 PM
Post#2


UtterAccess VIP
Posts: 5,451
Joined: 30-June 11



The following works fine for me:
!--c1-->
CODE
    Dim strComputer     As String
    Dim objWMIService   As Object
    Dim colItems        As Object
    Dim objItem         As Object
    strComputer = Me.PC_NAME 'Or simply strComputer = "." to query the local computer
    Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
                                  strComputer & "\root\cimv2")
    Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Service", , 48)
    Me.List45.AddItem "Service Name; Current State"
    For Each objItem In colItems
        Me.List45.AddItem objItem.Name & ";" & objItem.State
    Next
    Set objItem = Nothing
    Set colItems = Nothing
    Set objWMIService = Nothing

You listbox needs to be set to
Row Source Type: Value List
Column Count: 2
Column Widths: 2";1" -but this you can adjust to suit your exact needs
Column Heads: Yes
However, do you have the necessary permission to query other computers? This is normally lockdown in most companies (I know all the ones I worked for blocked this type of remote querying)!
Go to the top of the page
 
dspadic
post Jan 2 2014, 03:42 PM
Post#3



Posts: 181
Joined: 3-December 10
From: Columbus, Ohio


Following Daniel's example, you could set up a seperate function with a seperate module with this function in it:
ublic Function listProcess(Computername As String) As String
Dim strComputer As String
Dim objWMIService As Object
Dim colItems As Object
Dim objItem As Object
Dim strProcessList As String
strComputer = Computername 'Or simply strComputer = "." to query the local computer
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Service", , 48)
strProcessList = "Service Name; Current State"
For Each objItem In colItems
strProcessList = strProcessList & ";" & objItem.Name & ";" & objItem.state
Next
listProcess = strProcessList
Set objItem = Nothing
Set colItems = Nothing
Set objWMIService = Nothing
End Function
Then, in your form set the listbox.rowsource = listprocess(".") for local or listbox.rowsource = listprocess("Foreign Computername"), you can then use your code in other forms.
Example:
Private Sub btnlistwmidata_Click()
Me.wmiProcess.RowSource = listProcess(".")
Me.wmiProcess.Requery
End Sub
This will create two columns, one with the Service Name and one with the current state, populating the service name and state as the function goes through the services on the host, or foreign machine.
Go to the top of the page
 


Custom Search
RSSSearch   Top   Lo-Fi    13th December 2017 - 03:56 PM