Look "ahead" in a record set

User avatar
Gasman
2StarLounger
Posts: 104
Joined: 22 Feb 2022, 09:04

Re: Look "ahead" in a record set

Post by Gasman »

You really should have looked at and read that link I posted? :(

Then if you walk your code and test you would get

Code: Select all

Sub BKnight()
Dim rs As Recordset
Dim arrTest() As Variant
Dim db As DAO.Database

Set db = CurrentDb


Set rs = db.OpenRecordset("Select * from testtransactions")
rs.MoveLast
Debug.Print "Recordset count is " & rs.RecordCount
rs.MoveFirst
arrTest = rs.GetRows(rs.RecordCount)
Debug.Print "Array count is " & UBound(arrTest, 2) & "  Rows And "; UBound(arrTest, 1) & " Columns "
Set rs = Nothing
Set db = Nothing
End Sub
with output of

Code: Select all

Recordset count is 321

? ubound(arrtest,2)
 320 
Array count is 320  Rows And 8 Columns 
Using Access 2007/2019.
Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime.
Please, please use code tags when posting code snippets, click the </>icon.
Debug.Print is your lifesaver.

User avatar
HansV
Administrator
Posts: 78513
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Look "ahead" in a record set

Post by HansV »

I had already posted that link in the first reply in this thread... :sad:
Best wishes,
Hans

bknight
BronzeLounger
Posts: 1391
Joined: 08 Jul 2016, 18:53

Re: Look "ahead" in a record set

Post by bknight »

I used that link but that doesn't tell me how to load a full set of rows.

bknight
BronzeLounger
Posts: 1391
Joined: 08 Jul 2016, 18:53

Re: Look "ahead" in a record set

Post by bknight »

Gasman wrote:
20 Feb 2024, 02:42
You really should have looked at and read that link I posted? :(

Then if you walk your code and test you would get

Code: Select all

Sub BKnight()
Dim rs As Recordset
Dim arrTest() As Variant
Dim db As DAO.Database

Set db = CurrentDb


Set rs = db.OpenRecordset("Select * from testtransactions")
rs.MoveLast
Debug.Print "Recordset count is " & rs.RecordCount
rs.MoveFirst
arrTest = rs.GetRows(rs.RecordCount)
Debug.Print "Array count is " & UBound(arrTest, 2) & "  Rows And "; UBound(arrTest, 1) & " Columns "
Set rs = Nothing
Set db = Nothing
End Sub
with output of

Code: Select all

Recordset count is 321

? ubound(arrtest,2)
 320 
Array count is 320  Rows And 8 Columns 
I'm curious why the first rs? rs.GetRows(rs.RecordCount) and the second. Why is not a number inside the brackets not sufficient, or just RecordCount?

bknight
BronzeLounger
Posts: 1391
Joined: 08 Jul 2016, 18:53

Re: Look "ahead" in a record set

Post by bknight »

Code: Select all

Sub CalcProfit(Profit)
'This Function Will Calculate The Profit
Dim db As DAO.Database
Dim Rs As DAO.Recordset
Dim Fld1 As Field, Fld2 As Field, Fld3 As Field
Dim Fld4 As Field, Fld5 As Field, Fld6 As Field
Dim Fld7 As Field, Fld11 As Field, Fld12 As Field
Dim I As Long, intCurQty As Integer, intnextQty As Integer, intbackRec As Integer
Dim dblPrevAmt As Double, dblCurAmt As Double, dblQuantity As Double
Dim strCurSymbol As String, strCurCon As String
Dim recData As Variant
Set db = CurrentDb
Set Rs = db.OpenRecordset("Select * From NinjaTrader2024 Order By Time")
Set Fld1 = Rs!Time
Set Fld2 = Rs!Instrument
Set Fld3 = Rs!Quantity
Set Fld4 = Rs!Price
Set Fld5 = Rs!Amount
Set Fld6 = Rs!Profit
Set Fld7 = Rs!Action
Set fld8 = Rs!Commission
Set Fld9 = Rs!Ent_Ex
Rs.MoveFirst
Rs.MoveLast
Rs.MoveFirst
numrec = Rs.RecordCount
Debug.Print (numrec)
dblPrevAmt = 0
'recData(Field,Record) both zero based
'recData = Rs.GetRows(RecordCount, 16)Produces invalid property assignment
recData = Rs.GetRows(Rs.RecordCount)
numrows = UBound(recData) + 1
Debug.Print (numrows)
produces numrec=121;numrows=18
Not the entire recordset! Now what am I doing incorrectly?

User avatar
HansV
Administrator
Posts: 78513
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Look "ahead" in a record set

Post by HansV »

As gasman tried to tell you, you should use

numrows = UBound(recData, 2) + 1

UBound(recData) + 1 is equivalent to UBound(recData, 1) + 1; it returns the number of fields
Best wishes,
Hans

bknight
BronzeLounger
Posts: 1391
Joined: 08 Jul 2016, 18:53

Re: Look "ahead" in a record set

Post by bknight »

I guess I misread the Ubound statement, as I read it to display the number of rows. Yes there are 18 fields, that makes sense. Using numrows = UBound(recData, 2) + 1 produces 121 rows.
Whatever I did incorrectly, as it is difficult to remember the iterations I used, now
recData(7, I - 1), recData(7, I) and, recData(7, I + 1) all produce values instead of subscript out of range.
Thanks to both for the explanation.

bknight
BronzeLounger
Posts: 1391
Joined: 08 Jul 2016, 18:53

Re: Look "ahead" in a record set

Post by bknight »

Please explain this.
Two code snippets the first runs the second fails. Both are identical with the exception of recData has been commented out of the first.

Code: Select all

Rs.MoveFirst
Rs.MoveLast
Rs.MoveFirst
dblPrevAmt = 0
'recData(Field,Record) both zero based
'recData = Rs.GetRows(Rs.RecordCount)
For I = 1 To Rs.RecordCount
Rs.Edit

Code: Select all

Rs.MoveFirst
Rs.MoveLast
Rs.MoveFirst
dblPrevAmt = 0
'recData(Field,Record) both zero based
recData = Rs.GetRows(Rs.RecordCount)
For I = 1 To Rs.RecordCount
Rs.Edit

User avatar
SpeakEasy
4StarLounger
Posts: 558
Joined: 27 Jun 2021, 10:46

Re: Look "ahead" in a record set

Post by SpeakEasy »

GetRows moves the record pointer. Given you have instructed getrows to return ALL the records, that means the record pointer is at EOF. Hence you'll get a 'No current record' error with rs.Edit

User avatar
HansV
Administrator
Posts: 78513
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Look "ahead" in a record set

Post by HansV »

So insert

rs.MoveFirst

before rs.Edit
Best wishes,
Hans

bknight
BronzeLounger
Posts: 1391
Joined: 08 Jul 2016, 18:53

Re: Look "ahead" in a record set

Post by bknight »

HansV wrote:
20 Feb 2024, 13:02
So insert

rs.MoveFirst

before rs.Edit
Yes that works, but it does not answer why it fails, which is a question I forgot to ask in my previous post.

User avatar
HansV
Administrator
Posts: 78513
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Look "ahead" in a record set

Post by HansV »

See the reply by SpeakEasy.
Best wishes,
Hans

User avatar
Gasman
2StarLounger
Posts: 104
Joined: 22 Feb 2022, 09:04

Re: Look "ahead" in a record set

Post by Gasman »

HansV wrote:
20 Feb 2024, 08:01
I had already posted that link in the first reply in this thread... :sad:
Oops, sorry Hans. Still, perhaps worth reiterating to read it fully again?
Using Access 2007/2019.
Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime.
Please, please use code tags when posting code snippets, click the </>icon.
Debug.Print is your lifesaver.

User avatar
Gasman
2StarLounger
Posts: 104
Joined: 22 Feb 2022, 09:04

Re: Look "ahead" in a record set

Post by Gasman »

bknight wrote:
20 Feb 2024, 11:24
I guess I misread the Ubound statement, as I read it to display the number of rows. Yes there are 18 fields, that makes sense. Using numrows = UBound(recData, 2) + 1 produces 121 rows.
Whatever I did incorrectly, as it is difficult to remember the iterations I used, now
recData(7, I - 1), recData(7, I) and, recData(7, I + 1) all produce values instead of subscript out of range.
Thanks to both for the explanation.
Yes, on a single dimension array, that is all you would use, which TBH is the most common usage, at least that I have seen.
Using Access 2007/2019.
Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime.
Please, please use code tags when posting code snippets, click the </>icon.
Debug.Print is your lifesaver.

User avatar
HansV
Administrator
Posts: 78513
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Look "ahead" in a record set

Post by HansV »

Gasman wrote:
20 Feb 2024, 13:43
Oops, sorry Hans. Still, perhaps worth reiterating to read it fully again?
No need to apologize! It wasn't directed at you, but at bknight.
Best wishes,
Hans

bknight
BronzeLounger
Posts: 1391
Joined: 08 Jul 2016, 18:53

Re: Look "ahead" in a record set

Post by bknight »

HansV wrote:
20 Feb 2024, 13:04
See the reply by SpeakEasy.
I penned a reply to speakeasy, but failed to post it(got distracted)
Yes that works but it doesn't answer a question that I didn't ask in the previous post.
That question would be why? Now the question would be why the Rs.MoveFirst needs to be added again, we haven't done any Rs operations since the last Rs.MoveFirst just added a GetRows statement.

User avatar
HansV
Administrator
Posts: 78513
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Look "ahead" in a record set

Post by HansV »

As speakeasy mentioned, Rs.GetRows moves the record pointer by the number of records that it returns. Since Rs.GetRows(Rs.RecordCount) returns ALL records, it moves the record pointer of Rs to the end, after the last record. So using Rs.GetRows(Rs.RecordCount) effectively executes Rs.MoveLast too.
Best wishes,
Hans

User avatar
Gasman
2StarLounger
Posts: 104
Joined: 22 Feb 2022, 09:04

Re: Look "ahead" in a record set

Post by Gasman »

bknight wrote:
20 Feb 2024, 13:56
HansV wrote:
20 Feb 2024, 13:04
See the reply by SpeakEasy.
I penned a reply to speakeasy, but failed to post it(got distracted)
Yes that works but it doesn't answer a question that I didn't ask in the previous post.
That question would be why? Now the question would be why the Rs.MoveFirst needs to be added again, we haven't done any Rs operations since the last Rs.MoveFirst just added a GetRows statement.
From the link posted
The GetRows method will return fewer than the desired number of rows in two cases: either if EOF has been reached, or if GetRows tried to retrieve a record that was deleted by another user.
I must admit, I did not think about the record pointer being moved, as I just tested what was being retrieved, but if you think about it, it has to to do that to retrieve each records data.? To get to EOF it must be moving the record pointer?
Using Access 2007/2019.
Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime.
Please, please use code tags when posting code snippets, click the </>icon.
Debug.Print is your lifesaver.

bknight
BronzeLounger
Posts: 1391
Joined: 08 Jul 2016, 18:53

Re: Look "ahead" in a record set

Post by bknight »

You must be right but no recorset operations were performed in accessing all the records, just doesn't seem logical.

User avatar
HansV
Administrator
Posts: 78513
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Look "ahead" in a record set

Post by HansV »

From the link that both Gasman and I posted:
After you call GetRows, the current record is positioned at the next unread row. That is, GetRows has the same effect on the current record as Move numrows.
Best wishes,
Hans