• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 515
  • Last Modified:

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.
0
nomar2
Asked:
nomar2
  • 2
1 Solution
 
Rick_RickardsCommented:
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

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now