Cycling through a Multi Select ListBox with many selected items VBA ACCESS

This is the problem I am having... I have a form which has a MULTI-SELECT ( Simple ) listbox and a textbox.

Right now I have this on the 'Click' event. Not sure if this is correct event.

Basically I want to be able to highlight an invoice number or many invoice numbers from the listbox and have the total that is associated with those invoice numbers be placed in the textbox.


The problem I am having is the only the total for the first selected invoice number is being read and nothing after.

How do I cycle through a listbox so that all the selected items are read?? And after all the selected invoice numbers are read I want the grand total of those invoices to be placed in the textbox.

I think have to read the first selected index and then exit out of the loop and then read the next selected index..but I am a bit fuzzy about how to do it.

I have code that reads the selected item ..

      For intIndex = lstbx_status.ListCount - 1 To 0 Step -1
          If lstbx_status.Selected(intIndex) Then
             lnInvoiceNumber = lstbx_status.Column(0, intIndex)
          End If
      Next

but it doesn't only reads the first highlighted item from the listbox.

Any help or insight would be much appreciated.
nomar2Asked:
Who is Participating?
 
Rick_RickardsConnect With a Mentor Commented:
First, one should use the AfterUpdate event instead of the OnClick Event
Second, since you are only interested in the sum of items selected you don't need to loop thru the entire list box.  Instead there is a collection named .ItemsSelected ready made for this purpose.

Code illustrated below.  I'm assuming you're trying to output the total to a Text Box named "lnInvoiceNumber".

Hope this helps.

Rick

Private Sub lstbx_status_AfterUpdate()
    Dim varItem As Variant
    Dim curTotal As Currency
    With Me.lstbx_status
        For Each varItem In .ItemsSelected
            curTotal = curTotal + Nz(.Column(0, varItem),0)
        Next varItem
    End With
    Me.lnInvoiceNumber = curTotal
End Sub
0
 
nomar2Author Commented:
Wicked!!! That worked perfect!!!!!
0
 
Rick_RickardsCommented:
Glad I could help.

Rick
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.