Avatar of KimD2
KimD2
 asked on

VB6 Save multi selected listbox items to database

Hello,  I am trying to save all items in a multi selected listbox to a temporary table.  The code below creates the correct number of records but repeats the same data on each line.  What changes should I make to advance through the listbox and save each selected record?  I am using Visual Basic 6 and MS Access 2003.

Dim i As Integer
          For i = lstSelEmp.ListCount - 1 To 0 Step -1
            rstMessages.AddNew
                rstMessages("PersID") = lstSelEmp.ItemData(lstSelEmp.ListIndex)
                rstMessages("Message") = Me.txtMessage.Text
           rstMessages.Update
          Next

Thanks so much.  Kim
Visual Basic Classic

Avatar of undefined
Last Comment
KimD2

8/22/2022 - Mon
3_S

you have to loop through all the values in the listbox to see if it is selected or not.
I provide you with an example
3_S

my first post was too quick
i don't know if you change the value of Me.txtMessage.Text according to what is selected in the listbox
but instead you should use lstSelEmp.list() to get the corresponding value of itemdata
see attached code



Dim i As Integer
          For i = lstSelEmp.ListCount - 1 To 0 Step -1
            rstMessages.AddNew
                rstMessages("PersID") = lstSelEmp.ItemData(lstSelEmp.ListIndex)
                rstMessages("Message") = lstSelEmp.list(lstSelEmp.ListIndex)
           rstMessages.Update
          Next

Open in new window

KimD2

ASKER
Thank you for your response.  I am still having a problem.  Here is what I am trying to do.  There are 2 listboxes on my form. The items selected in listbox1 are moved to listbox2.  Now I need to save the itemdata from listbox2 temporary table ( a separate line for each item).   The value of txtMessage.text does not change.  
Could you revise my code to save every value in the listbox?  Kim  
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
bigjokey

I'm not sure if I understand your requirement, but if you only want to save the selected items from the listbox, then you need to check the selected property of each item before saving.  Refer to the attaced code snippet.
Dim i As Integer
For i = lstSelEmp.ListCount - 1 To 0 Step -1
	If lstSelEmp.Selected(i) = True Then
  	rstMessages.AddNew
    rstMessages("PersID") = lstSelEmp.ItemData(lstSelEmp.ListIndex)
    rstMessages("Message") = Me.txtMessage.Text
 		rstMessages.Update
 	End If
Next

Open in new window

KimD2

ASKER
Thank you for your sugestions.  If I have 4 items selected then this code saves the last item 4 times to the temp table.  I need to move through the listbox and save each item to the temp table.  (4 individual records).  I appreciate your patience on this.  Kim
3_S

does this fullfill your needs?
Dim i As Integer
For i = lstSelEmp.ListCount - 1 To 0 Step -1
	If lstSelEmp.Selected(i) = True Then
  	rstMessages.AddNew
    rstMessages("PersID") = lstSelEmp.ItemData(lstSelEmp.ListIndex)
    rstMessages("Message") = lstSelEmp.list(lstSelEmp.ListIndex)
 		rstMessages.Update
 	End If
Next

Open in new window

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
3_S

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
KimD2

ASKER
Thank you so much!