Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Update a Field from a Multiselect Listbox 1

Status
Not open for further replies.

kire1971

Technical User
May 13, 2003
6
US
I've created a form containing a Multiselect Listbox. The selections of the Listbox should be written to a field within the current record being edited or added.

The code i'm using updates the proper field but always to the first record in the table. How can I update the field in the current record?

Here's the code:

Private Sub Command49_Click()
Dim strVal As String
Dim varItem As Variant
Dim ctl As Control
Dim rst As DAO.Recordset
Set ctl = Me.CategoryList
For Each varItem In ctl.ItemsSelected
strVal = strVal & ctl.ItemData(varItem) & "~~"
Next
strVal = Mid(strVal, 1, Len(strVal) - 2)

Set rst = CurrentDb.OpenRecordset("Links")
rst.Edit
rst!AltCategories = strVal
rst.Update

Set rst = Nothing
Set ctl = Nothing
End Sub
 
How about looping through all items and check whether itemsselected = true.

Dim lngItem as Long
For lngItem = 0 to ctl.listcount -1
if ctl.itemselected then
strval=strval & "," & ctl.itemdata(lngItem)
end if
next

Vince
 
The code works fine in terms of Multiple Listbox entries. I'm getting the proper string. The problem is that the string always gets written to the first record in the table. I need to write it to the current record being edited.
 
I figured it out myself in case anyone else is looking for the answer...

Private Sub Command49_Click()
Dim strVal As String
Dim varItem As Variant
Dim ctl As Control
Set ctl = Me.CategoryList
For Each varItem In ctl.ItemsSelected
strVal = strVal & ctl.ItemData(varItem) & "~~"
Next
strVal = Mid(strVal, 1, Len(strVal) - 2)
[AltCategories] = strVal
Set ctl = Nothing
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top