Reputation: 873
I am using this SQL query
SELECT TOP 11 StockPrices.Close FROM StockPrices WHERE StockPrices.Date >=#2009-03-23# AND StockPrices.ID=1 ORDER by StockPrices.Date;
in VBA to fetch and store the data and keeping in a recordset
SQLQuery = "SELECT TOP 11 StockPrices.Close FROM StockPrices WHERE StockPrices.Date >=#" & tempDate & "# AND StockPrices.ID=" & tempId & " ORDER by StockPrices.Date;"
Set rst2 = db.OpenRecordset(SQLQuery)
Pd = rst2.Fields(0)
Pd1 = rst2.Fields(1)
Pd5 = rst2.Fields(5)
Pd11 = rst2.Fields(10)
rst2.Close
While Pd is being queried perfectly, but I am not able to access the rest of the data. As I understand, there is only one Field here, i.e the Close, which is coming up as Fields(0), I want to access the other data in the same field..
Let me give you an example, the queried result of the above SQL query is like this
3940
2544
5544
5633
1257
9855
3267
3141
2731
6931
SO I want the first, the second, the fifth and the tenth data in the listm which should be stored in Pd, Pd1,Pd5 and Pd11.
Any help, in how to do this?
Soham
Upvotes: 0
Views: 460
Reputation: 43219
With Pd1 = rst2.Fields(1)
you are accessing the second column of the current row.
If you want to access the second row, you need to use rst2.MoveNext
.
Below code would save all results to the results()
array.
You could access the fifth result with results(4)
.
Dim i as Integer
Dim results(11) as Integer
i = 0
Do while Not rst2.EOF
results(i) = rs2.fields(0)
i = i + 1
rst2.MoveNext
Loop
To move a number of steps you can use rst2.Move 4
i.e. for moving four steps.
The best way would be to only get the rows you need (1,5 and 11).
Upvotes: 1
Reputation: 2553
I am not that familiar with Access, but did you try storing the output into an Array and then referencing to the array values?
Cheers, Visakh
Upvotes: 0