Hi All,
I am new to Access and trying to set up a nested loop to cycle thru a table. I have a skeleton of the Sub created and can't control the second field, not knowing how to point the cursor at it. I have a field "Symbol" with about 3,000 entries, and a field "MktDate" with perhaps 70 entries for each value of Symbol. I want to cycle thru the entire table by incrementing Symbol, then run thru the MktDate field for that Symbol, go back and increment Symbol again, run thru MktDate again, each time doing some calculations, and will ultimately do an Edit Update to save some of the results. I believe I will need to convert from a Sub to a Function when I take that last step.
I've tried setting the sort order of the table, which doesn't seem to change anything. I can only get the first or last value of MktDate entries, so I know I am not controlling the cursor to increment that field with the value of Symbol fixed on any selection.
Performance isn't an issue. I will run this once a day, at day's end, on a powerful computer. It can run all night.
Any comments greatly appreciated.
Option Compare Database
Dim i As Integer
Dim j As Integer
Sub Single_Table_Nested_Loop()
Set dbs = CurrentDb
Set rs1 = dbs.OpenRecordset("ExportAAA", dbOpenDynaset)
Set rs2 = dbs.OpenRecordset("ExportAAA", dbOpenDynaset)
'Outer Loop to cycle thru "Symbol" field
With rs1
i = 2
'will replace with either the SymbolCount from the table query or EOF
'for now we will use a low value of i for testing
.MoveLast
.MoveFirst
Do While i > 0
Debug.Print "outer loop"; ";"
Debug.Print rs1.Fields("Symbol")
'we will loop thru the symbols and use the inner loop for each to calc ma's
j = 2
Do While j > 0
Debug.Print "inner loop"; ";"
With rs2
.MoveFirst
Debug.Print rs2.Fields("MktDate")
rs2.MoveNext
j = j - 1
End With
Loop
i = i - 1
rs1.MoveNext
Loop
End With
rs1.Close
Set rs1 = Nothing
Set rs2 = Nothing
Set db = Nothing
End Sub
I am new to Access and trying to set up a nested loop to cycle thru a table. I have a skeleton of the Sub created and can't control the second field, not knowing how to point the cursor at it. I have a field "Symbol" with about 3,000 entries, and a field "MktDate" with perhaps 70 entries for each value of Symbol. I want to cycle thru the entire table by incrementing Symbol, then run thru the MktDate field for that Symbol, go back and increment Symbol again, run thru MktDate again, each time doing some calculations, and will ultimately do an Edit Update to save some of the results. I believe I will need to convert from a Sub to a Function when I take that last step.
I've tried setting the sort order of the table, which doesn't seem to change anything. I can only get the first or last value of MktDate entries, so I know I am not controlling the cursor to increment that field with the value of Symbol fixed on any selection.
Performance isn't an issue. I will run this once a day, at day's end, on a powerful computer. It can run all night.
Any comments greatly appreciated.
Option Compare Database
Dim i As Integer
Dim j As Integer
Sub Single_Table_Nested_Loop()
Set dbs = CurrentDb
Set rs1 = dbs.OpenRecordset("ExportAAA", dbOpenDynaset)
Set rs2 = dbs.OpenRecordset("ExportAAA", dbOpenDynaset)
'Outer Loop to cycle thru "Symbol" field
With rs1
i = 2
'will replace with either the SymbolCount from the table query or EOF
'for now we will use a low value of i for testing
.MoveLast
.MoveFirst
Do While i > 0
Debug.Print "outer loop"; ";"
Debug.Print rs1.Fields("Symbol")
'we will loop thru the symbols and use the inner loop for each to calc ma's
j = 2
Do While j > 0
Debug.Print "inner loop"; ";"
With rs2
.MoveFirst
Debug.Print rs2.Fields("MktDate")
rs2.MoveNext
j = j - 1
End With
Loop
i = i - 1
rs1.MoveNext
Loop
End With
rs1.Close
Set rs1 = Nothing
Set rs2 = Nothing
Set db = Nothing
End Sub