We help IT Professionals succeed at work.
Get Started

VBA - SQL SELECT

129 Views
Last Modified: 2016-03-19
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
Comment
Watch Question
This problem has been solved!
Unlock 1 Answer and 4 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE