Using "for each next" statement to add records in table

D

DanMoraes

Dear mate,
I need to add records in table choosing more than one record from a form
list box.
Could you help me?
A lot of thanks.
 
O

Ofer

Try this

Private Sub Button_Click()
Dim prm_MyCtl As Control
Dim VarItm
Dim DBS As Database
Dim rst As Recordset

Set prm_MyCtl = Me![MyList]

If prm_MyCtl.ItemsSelected.Count = 0 Then
Beep
MsgBox "No Item Selected", 48
Exit Sub
End If
Set DBS = CodeDb
Set rst = DBS.openrecordset("Select * From MyTable")

' run through all selected items
For Each VarItm In prm_MyCtl.ItemsSelected
rst.addnew
rst!FieldName1 = prm_MyCtl.Column(0,VarItm) ' just choose the location
of the
rst!FieldName2 = prm_MyCtl.Column(1,VarItm)
field in the list start with 0
rst.update
Next

End Sub
 
Top