question

LucaTramontana-8177 avatar image
0 Votes"
LucaTramontana-8177 asked JackJJun-MSFT edited

ADODB error on filter: "Filter cannot be opened"

Dear all,

I'm using the below code done in VBA but I'm getting this error on this line code: RstProcess.Filter = "ProcessName = 'MSACCESS.EXE'"
193554-img-20220416-142841.jpg

Any way to solve this issue?


 Dim objSWbemServices As Object 

 Dim colSWbemObjectSet As Object 

 Dim objSWbemObject As Object 

 Dim StrComputer As String 

 Dim RstProcess As ADODB.Recordset 

 Dim CommandLineToSearch As String 

 Dim StrExecQuery As String 

 StrComputer = "." 

 StrExecQuery = "SELECT ProcessID, Name, CommandLine FROM Win32_Process WHERE Name = 'MSACCESS.EXE' and CommandLine Like '%Sample.accdb%'" 

  Set RstProcess = New ADODB.Recordset 

 RstProcess.Fields.Append "ProcessID", adVariant, , adFldMayBeNull 

 RstProcess.Fields.Append "ProcessName", adVariant, , adFldMayBeNull 

 RstProcess.Fields.Append "CommandLine", adVariant, , adFldMayBeNull 

 RstProcess.Open 

 Set objSWbemServices = GetObject("winmgmts:\\" & StrComputer & "\root\CIMV2") 

 Set colSWbemObjectSet = objSWbemServices.ExecQuery(StrExecQuery) 

 If colSWbemObjectSet.Count = 0 Then 

     MsgBox "Service is not running on target computer." 

 Else 

     For Each objSWbemObject In colSWbemObjectSet 

         RstProcess.AddNew 

         RstProcess.Fields("ProcessID").Value = CVar(objSWbemObject.ProcessID) 

         RstProcess.Fields("ProcessName").Value = CVar(objSWbemObject.Name) 

         RstProcess.Fields("CommandLine").Value = CVar(objSWbemObject.CommandLine) 

         RstProcess.Update 

     Next objSWbemObject 

     RstProcess.MoveFirst 

     RstProcess.Filter = "ProcessName = 'MSACCESS.EXE'" 

     Do Until RstProcess.EOF 

        Debug.Print RstProcess.Fields("ProcessID").Value 

        RstProcess.MoveNext 

     Loop 

 End If 

 RstProcess.Close 

 Set RstProcess = Nothing 

     
 Set objSWbemServices = Nothing 

 Set colSWbemObjectSet = Nothing 

 Set objSWbemObject = Nothing 


office-vba-devdotnet-adonet
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

Viorel-1 avatar image
0 Votes"
Viorel-1 answered Viorel-1 edited

Try changing two parts of your code:

 RstProcess.Fields.Append "ProcessID", adBigInt, , adFldMayBeNull
 RstProcess.Fields.Append "ProcessName", adBSTR, , adFldMayBeNull
 RstProcess.Fields.Append "CommandLine", adBSTR, , adFldMayBeNull

and

 RstProcess.Fields("ProcessID").Value = objSWbemObject.ProcessID
 RstProcess.Fields("ProcessName").Value = objSWbemObject.Name
 RstProcess.Fields("CommandLine").Value = objSWbemObject.CommandLine

By the way, you already have a WHERE Name = 'MSACCESS.EXE' filter.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.