View Full Version : Listbox Code Help


gmatriix
02-18-2008, 07:42 AM
Hello All,

I have this code that will calculate the totals from a query base off what is selected in the listbox. However, I am having trouble with totaling values together when I select two different items in the list box.

For example: If I select one item in the list box it gives me the total but when I select two or more it give me nothing. It is supposed to add the two totals together.

If I select CVBOOTS (for instance) it gives me

Ontime Pickup - 516
Late Pickup - 183
Ontime Delivery - 108
Late Delivery - 122

But..if I select FIBERTWR together with CVBOOTS it give me nothing

it should add the two totals together

Ontime Pickup - 516 +1003
Late Pickup - 183 + 367
Ontime Delivery - 108 + 2054
Late Delivery - 122 + 244

So the results should be

Ontime Pickup - 1519
Late Pickup - 550
Ontime Delivery - 2162
Late Delivery - 366

Here is the code:
Private Sub lstCName_Click()
'Declare Variables...
Dim Criteria$, i As Integer

'Enumerate through the List items so as to utilize
'those items selected.
For i = 0 To lstCName.ListCount - 1
'Is the List Item Selected?....
If lstCName.Selected(i) = True Then
'Yes it is...
'Does out Criteria Variable already contain something?
'If so, then place the AND Statement at the end of the
'String contained within the variable.
If Criteria <> "" Then Criteria = Criteria & " AND "
'Associate [CName] to the selected Item found in list.
Criteria = Criteria & "[CName]='" & Me.lstCName.ItemData(i) & "'"
End If
'See if there are more selected Items...
Next i

'Fill our Totals Into their Designated TexBoxes.
'We Use the DSum function to get the totals.
If Criteria <> "" Then
Me.[Ontime Pickup] = DSum("[SP Performance]![Ontime Pickup]", "[SP Performance]", Criteria)
Me.[Late Pickup] = DSum("[SP Performance]![Late Pickup]", "[SP Performance]", Criteria)
Me.[Ontime Delivery] = DSum("[SP Performance]![Ontime Delivery]", "[SP Performance]", Criteria)
Me.[Late Delivery] = DSum("[SP Performance]![Late Delivery]", "[SP Performance]", Criteria)
Else
Me.[Ontime Pickup] = 0
Me.[Late Pickup] = 0
Me.[Ontime Delivery] = 0
Me.[Late Delivery] = 0
End If
'Done
End Sub


Can any PLEASE HELP!

Thanks,

WayneRyan
02-18-2008, 08:18 AM
gmatriix,

Hard to follow the code, but I really think that:

If Criteria <> "" Then Criteria = Criteria & " AND "

The AND should be an OR. It's not possible for the field to be
"Late Pickup" AND "Ontime Delivery" at the same time.

Wayne

gmatriix
02-18-2008, 08:24 AM
GREAT!!!! AWESOME!!! EXCELLENT.......

Thanks A MILLION!!!!!!!!

WayneRyan
02-18-2008, 08:49 AM
gmatriix,

Glad to hear it.

btw,

Next time when you post code, format it like this:

{code}

Put your code in here

{/code}

BUT, change the braces to the square-brackets --> []

hth,
Wayne