B
Bill Stanton
Below is a simple routine that reads a table (Families) one
record at a time, extracts the family id from the key field and
proceeds to then generate 29 entries in a donations table for
that family. There are about 240 families, thus the resulting
donations table should have 29 x 240 = 6960 records when the
routine finishes. I watched the progress in the "Immediate
Window" via the Debug.Print and all appeared as expected.
However, the resulting donations table only has 29 records that
bares the ID of the first family, which happens to be one (1).
The results gives the appearance that the "For" loop only
executed once. That certainly isn't the case, as I've also observed
the Debug.Print placed "inside" the innermost loop.
What am I missing here?
Thanks,
Bill
Option Compare Database
Option Explicit
Dim con As ADODB.Connection
Dim rsFamilies As ADODB.Recordset
Private Sub MakeDonations()
Dim tmpSQL As String
Dim I As Integer
Dim DVal As Long
Dim FamID As Integer
Set con = Application.CurrentProject.Connection
tmpSQL = "SELECT [FamilyID] FROM [Families]"
Set rsFamilies = New ADODB.Recordset
rsFamilies.Open tmpSQL, con, adOpenKeyset, adLockOptimistic
While (Not (rsFamilies.EOF))
FamID = rsFamilies!FamilyID
DVal = 37815
For I = 1 To 29
tmpSQL = "INSERT INTO [Donations](FamilyID,DateVal)"
tmpSQL = tmpSQL & " VALUES(" & FamID & ", " & DVal & ");"
CurrentDb.Execute tmpSQL
DVal = DVal - 7
Next
Debug.Print tmpSQL
rsFamilies.MoveNext
Wend
rsFamilies.Close
Set rsFamilies = Nothing
End Sub
record at a time, extracts the family id from the key field and
proceeds to then generate 29 entries in a donations table for
that family. There are about 240 families, thus the resulting
donations table should have 29 x 240 = 6960 records when the
routine finishes. I watched the progress in the "Immediate
Window" via the Debug.Print and all appeared as expected.
However, the resulting donations table only has 29 records that
bares the ID of the first family, which happens to be one (1).
The results gives the appearance that the "For" loop only
executed once. That certainly isn't the case, as I've also observed
the Debug.Print placed "inside" the innermost loop.
What am I missing here?
Thanks,
Bill
Option Compare Database
Option Explicit
Dim con As ADODB.Connection
Dim rsFamilies As ADODB.Recordset
Private Sub MakeDonations()
Dim tmpSQL As String
Dim I As Integer
Dim DVal As Long
Dim FamID As Integer
Set con = Application.CurrentProject.Connection
tmpSQL = "SELECT [FamilyID] FROM [Families]"
Set rsFamilies = New ADODB.Recordset
rsFamilies.Open tmpSQL, con, adOpenKeyset, adLockOptimistic
While (Not (rsFamilies.EOF))
FamID = rsFamilies!FamilyID
DVal = 37815
For I = 1 To 29
tmpSQL = "INSERT INTO [Donations](FamilyID,DateVal)"
tmpSQL = tmpSQL & " VALUES(" & FamID & ", " & DVal & ");"
CurrentDb.Execute tmpSQL
DVal = DVal - 7
Next
Debug.Print tmpSQL
rsFamilies.MoveNext
Wend
rsFamilies.Close
Set rsFamilies = Nothing
End Sub