Use mulitselect listbox to create multiple records

T

Tim

Hello,
Is it possible to use a multi-select list box to create
multiple records in a table? For example: I would like
a form that contains a list box (CustomerNumber) and a
text box (Comment). I want to select multiple entries in
the CustomerNumber list box and enter text into the
Comment text box. I would then like to have records
written for each customer number I selected using the
text I entered.

To further illustrate:

If I select customer1, customer5, and customer14 in my
list box and enter "apple" in my text box, I would like
for three records to be created in my table. They would
look like:

RecordID CustomerNumber Comment

1 1 apple
2 5 apple
3 14 apple

Is this possible? I don't have to use a list box, that's
just all I know of that allows multiple selections.
Thanks in advance for your help.

Tim
 
J

John Vinson

Hello,
Is it possible to use a multi-select list box to create
multiple records in a table? For example: I would like
a form that contains a list box (CustomerNumber) and a
text box (Comment). I want to select multiple entries in
the CustomerNumber list box and enter text into the
Comment text box. I would then like to have records
written for each customer number I selected using the
text I entered.

Here's some code I've used in a similar situation; adapt it to your
objects as needed:

Private Sub cmdProcess_Click()
' Comments : Update the AnimalCondition table based on the
selections in
' the unbound multiselect listbox lstHealthIssues.
' Newly selected rows will be added to the table,
newly cleared
' rows will be deleted.
' Parameters: None
' Modified : 01/29/02 by JWV
'
' --------------------------------------------------
' Populate the AnimalCondition table with the selected issues
On Error GoTo PROC_ERR

Dim iItem As Integer
Dim lngCondition As Long
Dim db As DAO.Database
Dim rs As DAO.Recordset

' save the current record if it's not saved
If Me.Dirty = True Then
Me.Dirty = False
End If
Set db = CurrentDb
' Open a Recordset based on the table
Set rs = db.OpenRecordset("AnimalCondition", dbOpenDynaset)
With Me!lstHealthIssues
' Loop through all rows in the Listbox
For iItem = 0 To .ListCount - 1
lngCondition = .Column(0, iItem)
' Determine whether this AnimalID-HealthID combination is
currently
' in the table
rs.FindFirst "[AnimalID] = " & Me.AnimalID & " AND " _
& "[HealthIssueID] = " & lngCondition
If rs.NoMatch Then ' this item has not been added
If .Selected(iItem) Then
' add it
rs.AddNew
rs!AnimalID = Me.AnimalID
rs!HealthIssueID = lngCondition
rs.Update
End If ' if it wasn't selected, ignore it
Else
If Not .Selected(iItem) Then
' delete this record if it's been deselected
rs.Delete
End If ' if it was selected, leave it alone
End If
Next iItem
End With
rs.Close
Set rs = Nothing
Set db = Nothing
Me.subAnimalCondition.Requery

PROC_EXIT:
Exit Sub

PROC_ERR:
MsgBox "Error " & Err.Number & " in cmdProcess_Click:" _
& vbCrLf & Err.Description
Resume PROC_EXIT

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

Top