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

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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.
 
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top