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
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.
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
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?
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.
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.
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
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
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
I had already posted that link in the first reply in this thread...
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.
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.
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.
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.
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.
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.