Question Count items selected in list box

constantG

Registered User.
Local time
Today, 08:37
Joined
Jun 24, 2009
Messages
92
Hi,

The title doesn't really make up the question as there is a fair bit more to it but, here goes.

I have a listbox on a form which is populated by a query on a table with 6 columns.

One of the columns intTextLength is an integer containing the number 1 or 2.

I wish for the user to only be able to select rows in the listbox where intTextLength adds up to a maximum of 8

i.e. 8 rows where intTextLength = 1,
or 4 rows where intTextLength = 2
or combination of intTextLength like 2 rows where intTextLength = 2 and 4 rows where intTextLength = 1

I hope this all reads ok. Anyway I look forward to your reply.

Thanks
 
Here's code that adds up a value in the selected items and places the total in a textbox. You should be able to adapt it to your needs:

Code:
Private Sub lstCharges_AfterUpdate()
  Dim varItem As Variant
  Dim ctl As Control
  Dim curTotal As Currency

  Set ctl = Me.lstCharges
  curTotal = 0
 
  For Each varItem In ctl.ItemsSelected
    curTotal = curTotal + ctl.Column(4, varItem)
  Next varItem
  Me.txtTotalPayments = curTotal
  
  Set ctl = Nothing
End Sub
 
Thanks P :)

Works a treat. Only thing different was the column number and the type.

:)
 
No problem, glad it worked for you.
 
I have this working manually where the user is prompted if he/she selects too many items but are there any brains out there who can solve this automatically.

What I would like for it to acheive is this:

Say the listbox contained

four number 2's and twelve number 1's making a total of 20

I would want it to select 4 number 2's first and do something with them then
Select 8 number 1's and do something with those
and finally select the remaining 4 number 1's and do something with those.

This looks like an equation to me and I am hopeless when it comes to that. The code for the rest of it should be simple enough to work out once I've got the maths right.

Again I look forward to your reply.

Thanks
 
Solved:

Been playing around with this for, well the best part of 2 days now and I finally cracked it.

For anyone who'd be interested, I just had two queries, the first looked at records with both 1's and 2's and the second looked at just records that contained the 1's.

It is a little bit baiased towards the 2's but c'est la vie.

Thanks anyway.

;)
 

Users who are viewing this thread

Back
Top Bottom