Bouldergirl
Registered User.
- Local time
- Yesterday, 23:16
- Joined
- Feb 5, 2010
- Messages
- 11
Hello,
My problem is this: I have an input table (called rin in the following code) with a field (GDD) that I am hoping to create a running, cumulative sum of (in the output table field called “cum_GDD”) and pass these values into an output table (rout). I realize that VBA doesn’t have any conception of the operation it has performed in a previous iteration of a loop, and I’ve tried numerous tactics to overcome this, without success. The closest I’ve come thus far is to attempt to sum the current input table value (GDD) for the current “rin” record with the previous value calculated and passed into “rout”, using the function rout.MovePrevious. Unfortunately, I get the run-time error ‘3201’: No current record.
I guess I don’t completely understand how moving to a previous record works, but my strategy was to move to the previous iteration of the calculated cumulative sum of GDD (cum_GDD) in “rout”, hold this value, and then move to the next (presumably as yet unwritten) “rout” record to place the new sum into “rout”, using rout.MoveNext. I eventually tried to pull the previous “rout.cum_GDD” value out using dLookup and attempting to make the lookup criteria look for the previous record by equating this record to the field by which I sort the records (order matters in the running sum calculation, so I sort the input table by a field called “doy”, and this is about as far as I have gotten).
Does anyone have any suggestions for me? Please use lay-person lingo, as I’m not exactly a seasoned programmer. I don’t know if I could alternately pass the entire input table into an array and work with it that way, but I don’t know how to do that.
Thanks in advance for any and all help!
-Tiffany
Option Compare Database
[FONT="]Function Calc_cum_GDD()[/FONT]
[FONT="]Dim db As Database[/FONT]
[FONT="]Dim tdfNew As TableDef[/FONT]
[FONT="]Dim rin, rout, GDD_prev, GDD_cur[/FONT]
[FONT="]Dim today As Integer, yesterday As Integer[/FONT]
[FONT="]Dim GDD_cum As Single[/FONT]
[FONT="] ofilename = "LB_cum_GDD_Jan_Aug_b5"[/FONT]
[FONT="] Set db = CurrentDb()[/FONT]
[FONT="] For i = 0 To db.TableDefs.Count - 1 ' Delete table[/FONT]
[FONT="] If db.TableDefs(i).Name = ofilename Then[/FONT]
[FONT="] DoCmd.DeleteObject A_TABLE, ofilename[/FONT]
[FONT="] Exit For[/FONT]
[FONT="] End If[/FONT]
[FONT="] Next[/FONT]
[FONT="] Set tdfNew = db.CreateTableDef(ofilename)[/FONT]
[FONT="] With tdfNew[/FONT]
[FONT="] .Fields.Append .CreateField("doy", dbInteger)[/FONT]
[FONT="] .Fields.Append .CreateField("month", dbByte)[/FONT]
[FONT="] .Fields.Append .CreateField("day", dbInteger)[/FONT]
[FONT="] .Fields.Append .CreateField("GDD", dbSingle)[/FONT]
[FONT="] .Fields.Append .CreateField("cum_GDD", dbSingle)[/FONT]
[FONT="] db.TableDefs.Append tdfNew[/FONT]
[FONT="] End With[/FONT]
[FONT="] Set rin = db.OpenRecordset("GGD_cumulativeLB08312010", dbOpenDynaset)[/FONT]
[FONT="] [/FONT]
[FONT="] Set rout = db.OpenRecordset(ofilename, dbOpenDynaset)[/FONT]
[FONT="] [/FONT]
[FONT="] rin.Sort = "doy"[/FONT]
[FONT="] [/FONT]
[FONT="] rin.MoveFirst[/FONT]
[FONT="] [/FONT]
[FONT="] GDD_cum = rin.GDD[/FONT]
[FONT="] [/FONT]
[FONT="] cilf = GDD_cum[/FONT]
[FONT="] [/FONT]
[FONT="] rout.AddNew[/FONT]
[FONT="] rout![doy] = rin.doy[/FONT]
[FONT="] rout![month] = rin.month[/FONT]
[FONT="] rout![day] = rin.day[/FONT]
[FONT="] rout![GDD] = rin.GDD[/FONT]
[FONT="] rout![cum_GDD] = cilf[/FONT]
[FONT="] rout.Update[/FONT]
[FONT="] [/FONT]
[FONT="] rin.MoveNext[/FONT]
[FONT="] [/FONT]
[FONT="] Do Until rin.EOF[/FONT]
[FONT="] [/FONT]
[FONT="] For i = 0 To 1[/FONT]
[FONT="] [/FONT]
[FONT="] eilf = rin.doy - 1[/FONT]
[FONT="] [/FONT]
[FONT="] If rin.doy > 1 Then DLookup(rout.GDD, rout, rout.doy = eilf) = GDD_prev[/FONT]
[FONT="] [/FONT]
[FONT="] GDD_cur = rin.GDD[/FONT]
[FONT="] [/FONT]
[FONT="] GDD_cum = GDD_prev + GDD_cur[/FONT]
[FONT="] [/FONT]
[FONT="] cilf = GDD_cum[/FONT]
[FONT="] [/FONT]
[FONT="] rout.Edit[/FONT]
[FONT="] rout.cum_GDD = cilf[/FONT]
[FONT="] rout.Update[/FONT]
[FONT="] [/FONT]
[FONT="] rin.MoveNext[/FONT]
[FONT="] [/FONT]
[FONT="] Next i[/FONT]
[FONT="] Loop[/FONT]
[FONT="] [/FONT]
[FONT="] rin.Close: rout.Close[/FONT]
[FONT="]End Function[/FONT]
My problem is this: I have an input table (called rin in the following code) with a field (GDD) that I am hoping to create a running, cumulative sum of (in the output table field called “cum_GDD”) and pass these values into an output table (rout). I realize that VBA doesn’t have any conception of the operation it has performed in a previous iteration of a loop, and I’ve tried numerous tactics to overcome this, without success. The closest I’ve come thus far is to attempt to sum the current input table value (GDD) for the current “rin” record with the previous value calculated and passed into “rout”, using the function rout.MovePrevious. Unfortunately, I get the run-time error ‘3201’: No current record.
I guess I don’t completely understand how moving to a previous record works, but my strategy was to move to the previous iteration of the calculated cumulative sum of GDD (cum_GDD) in “rout”, hold this value, and then move to the next (presumably as yet unwritten) “rout” record to place the new sum into “rout”, using rout.MoveNext. I eventually tried to pull the previous “rout.cum_GDD” value out using dLookup and attempting to make the lookup criteria look for the previous record by equating this record to the field by which I sort the records (order matters in the running sum calculation, so I sort the input table by a field called “doy”, and this is about as far as I have gotten).
Does anyone have any suggestions for me? Please use lay-person lingo, as I’m not exactly a seasoned programmer. I don’t know if I could alternately pass the entire input table into an array and work with it that way, but I don’t know how to do that.
Thanks in advance for any and all help!
-Tiffany
Option Compare Database
[FONT="]Function Calc_cum_GDD()[/FONT]
[FONT="]Dim db As Database[/FONT]
[FONT="]Dim tdfNew As TableDef[/FONT]
[FONT="]Dim rin, rout, GDD_prev, GDD_cur[/FONT]
[FONT="]Dim today As Integer, yesterday As Integer[/FONT]
[FONT="]Dim GDD_cum As Single[/FONT]
[FONT="] ofilename = "LB_cum_GDD_Jan_Aug_b5"[/FONT]
[FONT="] Set db = CurrentDb()[/FONT]
[FONT="] For i = 0 To db.TableDefs.Count - 1 ' Delete table[/FONT]
[FONT="] If db.TableDefs(i).Name = ofilename Then[/FONT]
[FONT="] DoCmd.DeleteObject A_TABLE, ofilename[/FONT]
[FONT="] Exit For[/FONT]
[FONT="] End If[/FONT]
[FONT="] Next[/FONT]
[FONT="] Set tdfNew = db.CreateTableDef(ofilename)[/FONT]
[FONT="] With tdfNew[/FONT]
[FONT="] .Fields.Append .CreateField("doy", dbInteger)[/FONT]
[FONT="] .Fields.Append .CreateField("month", dbByte)[/FONT]
[FONT="] .Fields.Append .CreateField("day", dbInteger)[/FONT]
[FONT="] .Fields.Append .CreateField("GDD", dbSingle)[/FONT]
[FONT="] .Fields.Append .CreateField("cum_GDD", dbSingle)[/FONT]
[FONT="] db.TableDefs.Append tdfNew[/FONT]
[FONT="] End With[/FONT]
[FONT="] Set rin = db.OpenRecordset("GGD_cumulativeLB08312010", dbOpenDynaset)[/FONT]
[FONT="] [/FONT]
[FONT="] Set rout = db.OpenRecordset(ofilename, dbOpenDynaset)[/FONT]
[FONT="] [/FONT]
[FONT="] rin.Sort = "doy"[/FONT]
[FONT="] [/FONT]
[FONT="] rin.MoveFirst[/FONT]
[FONT="] [/FONT]
[FONT="] GDD_cum = rin.GDD[/FONT]
[FONT="] [/FONT]
[FONT="] cilf = GDD_cum[/FONT]
[FONT="] [/FONT]
[FONT="] rout.AddNew[/FONT]
[FONT="] rout![doy] = rin.doy[/FONT]
[FONT="] rout![month] = rin.month[/FONT]
[FONT="] rout![day] = rin.day[/FONT]
[FONT="] rout![GDD] = rin.GDD[/FONT]
[FONT="] rout![cum_GDD] = cilf[/FONT]
[FONT="] rout.Update[/FONT]
[FONT="] [/FONT]
[FONT="] rin.MoveNext[/FONT]
[FONT="] [/FONT]
[FONT="] Do Until rin.EOF[/FONT]
[FONT="] [/FONT]
[FONT="] For i = 0 To 1[/FONT]
[FONT="] [/FONT]
[FONT="] eilf = rin.doy - 1[/FONT]
[FONT="] [/FONT]
[FONT="] If rin.doy > 1 Then DLookup(rout.GDD, rout, rout.doy = eilf) = GDD_prev[/FONT]
[FONT="] [/FONT]
[FONT="] GDD_cur = rin.GDD[/FONT]
[FONT="] [/FONT]
[FONT="] GDD_cum = GDD_prev + GDD_cur[/FONT]
[FONT="] [/FONT]
[FONT="] cilf = GDD_cum[/FONT]
[FONT="] [/FONT]
[FONT="] rout.Edit[/FONT]
[FONT="] rout.cum_GDD = cilf[/FONT]
[FONT="] rout.Update[/FONT]
[FONT="] [/FONT]
[FONT="] rin.MoveNext[/FONT]
[FONT="] [/FONT]
[FONT="] Next i[/FONT]
[FONT="] Loop[/FONT]
[FONT="] [/FONT]
[FONT="] rin.Close: rout.Close[/FONT]
[FONT="]End Function[/FONT]