troubleshooting Question

VBA - SQL SELECT

Avatar of W.E.B
W.E.B asked on
Microsoft ExcelVisual Basic Classic
4 Comments1 Solution133 ViewsLast Modified:
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
ASKER CERTIFIED SOLUTION
David Christal CISSP

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 4 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros