ADODB error on filter: "Filter cannot be opened"

Luca Tramontana 21 Reputation points
2022-04-16T13:08:02.867+00:00

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   
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,650 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Viorel 114.7K Reputation points
    2022-04-16T14:55:49.18+00:00

    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.

    0 comments No comments