0

Hope you can help - I have simple list box which is populated from DB and has 6 columns [ID, name, ordnumber, data, price ,postcode]

The listbox is populated without issues [So for that purposes I will not post the code]

The issue i have is when i'm trying to get the running total of the prices when I select item in list box.

 With lstOrdersInBatch
   tbxTotal.value = 0
    For IntIndex = 0 To .ListCount - 1
        If .Selected(IntIndex) Then
         'tbxTotal = tbxTotal + .Column(4)
   
           
        End If

    Next
End With

It's giving me incorrect value

I have gone through the debug log and it's seems it only take the last item selected & not all selected.

Hope you can help.

Thanks

Please note is access VBA programming.

===== SOLVED ===

Retrieve column values of the selected row of a multicolumn Access listbox

That has the answer.

       With lstOrdersInBatch
       tbxTotal.value = 0
       For IntIndex = 0 To .ListCount - 1
            If .Selected(IntIndex) Then
            tbxTotal = tbxTotal + lstOrdersInBatch.Column(4, IntIndex)
            End If
    
       Next
   End With

Thank you for trying.

Davesexcel
  • 6,896
  • 2
  • 27
  • 42
Azmodan
  • 107
  • 2
  • 10

2 Answers2

1

Alright, I've adjusted the code based on your comment:

Dim i As Integer
For i = 0 To Me.lstOrdersInBatch.ListCount - 1
    If Me.lstOrdersInBatch.Selected(i) Then
        tbxTotal = tbxTotal + Me.lstOrdersInBatch.ItemData(i)
    End If
Next i
Wintermute
  • 136
  • 9
  • Thanks Winter, however - i made silly mistake and didn't report that this is access VBA programming - as ListItem is not picked up by access :( – Azmodan Jan 17 '18 at 20:46
0

You can reduce the number of iterations and avoid the need for the if statement using the following:

Dim idx
tbxTotal = 0
With lstOrdersInBatch
    For Each idx In .ItemsSelected
        tbxTotal = tbxTotal + .Column(4, idx)
    Next idx
End With
Lee Mac
  • 15,615
  • 6
  • 32
  • 80