VBA - SQL SELECT

Hello,
I'm using a  cnn = New ADODB.Connection, to get some data from SQL SERVER 2008R2
When I run my simple select statment below, I'm getting thousands of records back. (in the sample attached, I should have received back the first  3 records back only).
if I run the same script inside SQL, I get the proper results. (one record per Tracking).

Any help is appreciated.

    Dim cnt As Integer
    Dim i As Integer
 For i = 2 To Range("A2").End(xlDown).Row + 1
 cnt = cnt + 1
 
' Start
cnn.Open "Provider=SQLOLEDB.1;User ID= xxxxxxxxxxxxxxxxxxxx

sql = "Select OrderNo, " & _
"NoteNumber from FinalizedOrderNotes " & _
"where NoteText Like ('%" & sh.Cells(i, 1) & "%') "

    rs.Open sql, cnn, adOpenDynamic, adLockOptimistic
    sh.Cells(i, 2).CopyFromRecordset rs
    rs.Close
    cnn.Close
   
     Next i

Thank you,
SAMPLE.xls
W.E.BAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David Christal CISSPCommented:
It looks like that if your target cell is empty your filter evaluates to '%%' with is no filter at all, explaining the too many records problem.  Also, it looks like you're pulling from column A instead of column B .
W.E.BAuthor Commented:
Hello,
This is the way it's supposed to be,
Column "A" is the noteText that I'm looking up in SQL
Results will be in column "B".

The main question I guess  is why is it not stopping and ending the macro after the last row used in Column "A"

Thanks,
David Christal CISSPCommented:
How about something like this?

'
Range("a2").Select
Range(Selection, Selection.End(xlDown)).Select
RowCount = Selection.Rows.Count
Dim i As Integer
For i = 2 To RowCount + 1
    cnn.Open "Provider=SQLOLEDB.1;User ID= xxxxxxxxxxxxxxxxxxxx"
    Sql = "Select OrderNo, " & _
        "NoteNumber from FinalizedOrderNotes " & _
        "where NoteText Like ('%" & sh.Cells(i, 1) & "%') "
    rs.Open Sql, cnn, adOpenDynamic, adLockOptimistic
    sh.Cells(i, 2).CopyFromRecordset rs
    rs.Close
    cnn.Close
Next i

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
W.E.BAuthor Commented:
Awesome,
Thank you very much.
It worked.,
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.