Question: In Acumatica project , on Receipt screen have a lookup button "Add Item". When Inventory lookup show dialog ( it's FromDetail template ), i will scan barcode from my bill. But, i must scan second times it to working.
How can i to filter grid details and set checked a row filtered according barcode ? And how can to know row count filtered? . Please help me. 

The below figure illustrates.

[snip code](https://i.stack.imgur.com/1e71i.png)

[Screen Receipt](https://i.stack.imgur.com/3Ue3g.png)

Answer: 
**Parfait** provided a solution for me in the comments to my original question. Changing my connection string provider to a driver did not work. It caused a `Run-time error '-2147467259 (80004005)'`

Instead of a CTE, I utilized a derived table in my query.

The SQL query that returns a recordcount for me in VBA is:

```
SELECT main.*
FROM (SELECT *, ROW_NUMBER() OVER (PARTITION BY [partNumber] 
ORDER BY [date] DESC) AS i FROM [myDB].[dbo].[PartOrders] 
WHERE [partDescription] like '%motor%') 
AS main WHERE main.i = 1

```

Thank You!
I had the same issue except I was using Driver as recommended by user7638417. When the recordset returned, the recordcount was -1, but, when I inserted the rows into the spreadsheet, they were all there (24). Used simplified query in example, actual query was more complex which lead me to the CTE method.

```
    Dim sql As String
    Dim dbConn As New ADODB.Connection
    Dim dbRS As New ADODB.Recordset
    Dim rows As Integer

    sql = "WITH S1 AS ( SELECT Lot, CollectDt, Mark, Slot, Thick1, Thick2 From Table1 )"
    sql = sql + " WHERE Lot = 'lotnumber' "
    sql = sql + " SELECT * FROM S1 WHERE Thick2 <> "UNKNOWN" ORDER BY Slot "

    dbConn.ConnectionString = "Driver={SQL Server};server=myserver;database=production;uid=guest;pwd=guest"
    dbConn.Open
    dbConn.CommandTimeout = 600
    dbRS.Open sql, dbConn, adOpenStatic, adLockReadOnly

    Dim totRow As Integer
    totRow = dbRS.RecordCount + 2

   ' If (dbRS.RecordCount > 0) Then
        Sheet1.Cells(2, 1).CopyFromRecordset dbRS
   ' End If

```

I had to comment out the check for data in order to get the data from the record set. The value in totRow came back as 1 instead of 26, the expected value which causes other things to fail later in the code.
SQL Server Version: 13.0.4001.0
Excel Version: 2016