Reputation: 120
What's the easiest way of adding new records to a table based on the number selected in a combobox? I.E. I have selected the number 6 in a combobox and want it to add 6 records to a table, all with the same information.
Right now I'm using recAdd but by doing so I have to add code for each number that could be selected. If I choose 6 then I'm doing an if statement for 1 through 6.
Upvotes: 0
Views: 845
Reputation: 55806
Use the function here: Duplicate Record with New Primary Key (VBA)
Wrap the add/update in a loop:
With rstInsert
For i = 1 to CountOfNewRecords ' set to value from your combobox
.AddNew
For Each fld In rstSource.Fields
With fld
If .Attributes And dbAutoIncrField Then
' Skip Autonumber or GUID field.
ElseIf .Name = "SomeFieldToPreset"
rstInsert.Fields(.Name).Value = SomeValue
ElseIf .Name = "SomeFieldToExclude"
' Leave blank
Else
' All other fields.
' Copy field content.
rstInsert.Fields(.Name).Value = .Value
End If
End With
Next
.Update
Next
' Go to the new record and sync form.
.MoveLast
Me.Bookmark = .Bookmark
.Close
End With
Upvotes: 1