I am querying the excel using the below code to get the rows matching a certain condition:
For i = 90 To Worksheets("My Sheet").Range("E65536").End(xlUp).Row
' This for loop is to iterate through the list of dates in the excel sheet
Value = Worksheets("My Sheet").Range("E" & i).Value - #1/1/1900# + 2
'Using the above line i am converting the date i want to filter the date
'into numeric
strQuery = "SELECT * FROM [My another sheet$A3:I" & LastRow & "] WHERE [Date1]< " &
Value & " AND ([somefield] = 'dog' or [somefield] = 'cat')"
' Here in the SQL query i am trying to see how many cats and dogs are satisfying with my date criteria
Set rs = New ADODB.Recordset
With rs
.ActiveConnection = cn
.Source = strQuery
.CursorType = adOpenDynamic
.CursorLocation = adUseClient
.LockType = adLockOptimistic
.Open
End With
' Executing the select query on excel as database using ADODB api
If rs.RecordCount = 0 Then
Else
rs.MoveLast
End If
Worksheets("My Sheet").Range("F" & i).Value = rs.RecordCount
rs.Close
' Moving to the next date in the list
Next
I have data something like this
- 01/01/2012 dog A
- 02/01/2012 cat B
- 03/01/2012 cat C
- 04/01/2012 rat D
- 05/01/2012 cat E
- 06/01/2012 parrot F
and my list has dates like this
- 12/31/2010 - Here i have to find how many cats and dogs are purchased from the shop with date less than this date
- 12/31/2011 - Here i have to find how many cats and dogs are purchased from the shop with date less than this date
The purpose of the for loop is to iterate through these dates and modifying the select query This are the values i am receiving from the above code
32
35
37
44
57
64
71
78
84
86
89
91
91
But i should get these values
32
36
37
47
57
66
73
81
84
89
90
91
91
I read somewhere like that we should issue the command rs.movelast
before using the record count property but still it is showing wrong result on some iterations..
Is it because of the loop and i am using the same record set.
The reason i know the count should be is because i just did the same filtering on excel columns and it is showing different count
There is no datatype changing in the columns i.e. each column has same data structure from start to end
Please help me on this.
I hope the question is clear..please let me know if more details are needed.