By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,165 Members | 853 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,165 IT Pros & Developers. It's quick & easy.

Solution to total a specific column in a list box

convexcube
P: 47
After searching the web for a solution to total a specific column in a list box and not finding it, I came up with this:

Expand|Select|Wrap|Line Numbers
  1. Dim varTotal As Currency
  2. Dim varRow As Integer
  3. For varRow = 1 To (lstListbox.ListCount - 1)
  4. varTotal = varTotal + lstListbox.Column(2, varRow)
  5. Next
  6. txtTotal = varTotal
This will provide a total in txtTotal for the third column (referenced as "2" because the first column is "0") in the list box as currency.
  • Change "lstListbox" to your list box name
  • Ensure you have an unbound text box called "txtTotal" on the form. (or call it something else, but be sure to change the code in line 5 to reference it)
  • To change to a different column, alter the "2" in line 4.
  • I have Column Heads set to "Yes" in my list box which requires a "-1" to be placed in row 3. Remove this if there are no Column Heads.
  • If totaling a different data type, change "Currency" in line 1 to your data type.

Hope this can provide some help to someone.

Regards,
Ken.
Dec 19 '07 #1
Share this Question
Share on Google+
6 Replies


missinglinq
Expert 2.5K+
P: 3,532
The reason searching the web for a solution to "total a specific column in a list box" came up empty is very simple: Listboxes are meant to be used to allow users to select data for inclusion in a record, not for simply displaying data! If used properly, there is no valid reason to sum up a Listbox column's data!

You should have been using a subform to display your data, in which case the summing of a column would have been very simple.

Linq ;0)>
Jan 5 '08 #2

convexcube
P: 47
The reason searching the web for a solution to "total a specific column in a list box" came up empty is very simple: Listboxes are meant to be used to allow users to select data for inclusion in a record, not for simply displaying data! If used properly, there is no valid reason to sum up a Listbox column's data!

You should have been using a subform to display your data, in which case the summing of a column would have been very simple.

Linq ;0)>
I really don't think there's any need to be so condescending. I never said I wasn't using the list box to select data. It can still be useful for the user to see a total. At any rate all I was trying to do was provide the solution I came up with so anyone who might want to do it, but is not sure how, can. If people are denigrated for being creative then innovation will cease for sure.

Regards,
Ken.
Jan 5 '08 #3

missinglinq
Expert 2.5K+
P: 3,532
I wasn't being condescending, Ken, merely pointing out to you, and to others who might read the thread, that Listboxes are not intended to be used as primary data display objects!

In responding to questions posted here, we have two objectives:



  1. To aid the original poster in solving his/her particular problem
  2. To provide information on the subject that may be helpful to others who come upon the thread
Because of the second objective listed above, it would be remiss of me not to comment on the inappropriateness of using Listboxes in this manner. Others coming along could get the idea that this was an acceptable usage, and that's simply not true.

Pointing out mistakes is never done to denigrate the poster, it's simply done so that others may avoid making the same ones! Innovation and creativity is what programming is all about, and we would never intentionally do anything here to discourage it!

Lastly, as a point of order, the technical forums here at TSDN are for the posting of questions and/or problems, and as such are not the proper venues for posting instructional threads. We are always happy to receive and evaluate such threads, but they should be posted in the HowTo section of the site.

Despite the wrongness of using Listboxes for the primary display of data, it's truly a pleasure to come across someone like yourself, who actually attempts to solve their own problems, instead of simply posting them and saying "Someone please do my work for me!"

I look forward, Ken, to seeing your future contributions here at TheScripts.

Linq ;0)>
Jan 6 '08 #4

convexcube
P: 47
Hello MissingLinq,

First of all, thanks for pointing out that this should have been posted in the how-to section. Although I now know this is the place the original post should have gone, at the time I didn't.

Secondly, I realise that it wasn't your intention to be condescending and denigrate this solution but I feel that the words you chose completely negated what I had to say. Although you describe using listboxes in this way is "not valid", "inappropriate", "unacceptable" and "wrong", the solution I came up with works and is valid. I agree that you should point out better solutions, but its definitely beneficial to recognise what people have achieved - especially if they are just starting out.

Lastly, I think this website is wonderful and has been an invaluable source of information. It is so beneficial to have such experts willing to share their knowledge and help newbies become more all in an open and free environment.


Kind Regards,
Ken.
Jan 10 '08 #5

P: 1
Although this is about 3 1/2 years later, I needed to thank you for this solution to a problem I was having in 2011.

Keep on being creative...:)

Al
Mar 22 '11 #6

NeoPa
Expert Mod 15k+
P: 31,494
I presume you read the rest of the thread Al, so I won't repeat the warning that Linq has already expressed. I assume you have already considered and disregarded this advice (which is entirely your right of course). We can only offer the benefits of our experience. We cannot force the horse to drink, as it were.

That said, I know thread resurrection is often frowned upon, but I appreciate the impulse that prompted you to thank ConvexCube for what is, after all, an innovative solution.
Mar 23 '11 #7

Post your reply

Sign in to post your reply or Sign up for a free account.