Here's a bit of code that loops through a table, creating multiple records in another table:
Rich (BB code):
Function MakeLabels()
Dim db As DAO.Database
Dim rst1 As DAO.Recordset
Dim rst2 As DAO.Recordset
Dim strSQL As String
Dim Counter As Integer
Dim txt1 As String
Dim txt2 As String
Dim txt3 As String
Dim txt4 As String
Dim txt5 As String
Dim txt6 As String
Dim i As Integer
'Initialise objects
Set db = CurrentDb()
Set rst1 = db.openrecordset("People")Set rst2 = db.openrecordset("LabelData")
'Create Delete statement to clean out existing LabelData records
strSQL = "DELETE * FROM LabelData;"
DoCmd.SetWarnings False 'Turn deletion warning off
DoCmd.RunSQL (strSQL)
With rst1
.MoveFirst 'go to first record
Do Until .EOF 'move through all records in table
txt1 = !firstname
txt2 = !lastname
txt3 = !Address
txt4 = !city
txt5 = !State
txt6 = !postcode
Counter = !labels
With rst2
For i = 1 To Counter
.AddNew
!firstname = txt1
!lastname = txt2
!Address = txt3
!city = txt4
!State = txt5
!postcode = txt6
.Update
Next i
End With
.MoveNext
Loop
End With
DoCmd.SetWarnings True 'Turn deletion warning back on
End Function
Some explanation:
People is the source table
LabelData is the destination table
Both have an ID field; People also has a Labels (Integer) field.
Both have in common the following fields (all text);
FirstName
LastName
Address
City
State
Postcode
The code goes to the first record in People, writes the field values out to the 6 txt variables and the Counter, and creates as many records in LabelData as the value in Counter.
I cheated a bit
because I should have based the source records on a query, but I wsa trying to show how you loop through and build the records.
The bit in red could be replaced by the following, to use a query instead:
Place these in the declarations
Rich (BB code):
Dim qdf as DAO.QueryDef
Dim prm as DAO.Parameter
Replace the red text with this
Rich (BB code):
Set qdf = db.QueryDefs("qryWhichLabels")
For Each prm In qdf.Parameters
prm.Value = Eval(prm.Name)
Next prm
Set rst1 = qdf.OpenRecordset
When the records are created you can display a report based on LabelData