0

Trying to work a RecordSet count in VBA Using ADODB recordset but it won't seem to get the count to work properly.

I've got 50 records in a worksheet with unique ID's, some of them are already in a database and some are not, the code itself is to loop through each cell and get the value of the cell and run that through an SQL select statement if theres a hit then say found if not then say nothing.

Unfortunately it seems to return the same result for everything, even the id's i know do not exist.

Code bellow

Sub NO6_ChequeCheck()

Dim con As ADODB.Connection
Dim rec As ADODB.Recordset

Set con = New ADODB.Connection
Set rec = New ADODB.Recordset


Dim sql As String
Dim client As String

Dim myRange As Range
Dim myCell As Range

Set myRange = Range("A2:A52")

Dim i As Integer
i = 2

With con
    .Provider = "MSDASQL"
    .ConnectionString = "DSN=localhostTest"
    .Open
End With

For Each myCell In myRange

    client = myCell.text

    sql = "SELECT * FROM crm_client_cheques WHERE id = '" & client & "' "

    rec.Open sql, con

    If rec.RecordCount = 0 Then
        Cells(i, "H").Value = "Nothing"
    Else
        Cells(i, "H").Value = "Found"
    End If

    rec.Close

    i = i + 1

Next myCell

The main thing I've come across is that if i toggle that 0 to say 50 and mess with the = sign and change it to < or > then the results will change to either , which leads me to believe its not resetting the recordcount back so it just stacks each time so its always going up each loop and not going back to 0.

i have tried telling it to close and equal nothing and moved various bits of code around but nothing helps much.

All help appriciated

6
  • This link might help you: stackoverflow.com/questions/2282661/… Commented Aug 26, 2014 at 15:34
  • is it the same when you remove the single quotes from around the client variable Commented Aug 26, 2014 at 15:38
  • In addition to cyboashus answer, see allenbrowne.com/ser-29.html (Trap 4). Commented Aug 26, 2014 at 15:40
  • The count method only works once you have looped through all the data. See the answer below from user3793865 about .bof and .eof that's the correct way to do it. Commented Aug 27, 2014 at 9:29
  • And your code is going to be very slow if you repeatedly keep querying a recordset inside that loop. Why don't you query the recordset before you loop through the cells and then search the recordset for the value inside the loop. Commented Aug 27, 2014 at 9:31

1 Answer 1

3

You need to check whether there are any records returned by your sql query. You can do that by

 If Not (rec.BOF And rec.EOF) Then 'There are no records
      Cells(i, "H").Value = "Nothing"
 else
      Cells(i, "H").Value = "Found"
 End if
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.