Listbox sorting and comparing and tally

Posted on 2002-06-11
Medium Priority
Last Modified: 2011-09-20
I know I can do this but time is not permitting so perhaps someone here may have a snippet of code that will come close to what I need.

I have a list box with a lsit of things.
Example List:
1/4 Hex Nut
1/3 nut
Wall screw Alloy
3/4 bolt
1/3 Hex Nut
Cement Nail
2/3 Lug Nut
1/2 Driver nut
Dry wall nail
Finishing nail
1/4 Ply wood
Hex Screw Black

Okay, we have the list.  Here is what I need to do.  I need to tally how many of each item are there.  Keep in mind the list will change from time to time. So, I need a tally of how many general categories I have so the return should come back 5-Nut, 2-Screw, 1-Bolt, 2-Nail, 1-Wood

Perhaps I will have to create a definitions table of general items, so lets assume I have that for the list to relate to.  if there is a way to do this without using a definition table then the better because this list is very dynamic.

Okay, I think you have the scenario.  If you need more info please ask away.
Question by:alariac
  • 3
  • 3
  • 2
  • +1

Expert Comment

ID: 7069677
Hi. Are You SURE You want this to be done Your way? I mean things of this sort usualy are handled a different way.

Anyway, If You want, It is possible to be done, but there are some restrictions. You must have a simmilar format for each item in the list. I mean:
"Wall Screw Alloy" must become "Alloy Wall Screw"
"Hex Screw Black" must become "Black Hex Screw"

Then it is easy - if you ensure that the last word identifies the item itself, and the oders are just some 'properties' of this item...
I can send you some code, If this restriction is ok for you.

LVL 28

Expert Comment

ID: 7069723
Dim Nails As Integer
Dim Bolts As Integer
Dim Wood As Integer
Dim Screws As Integer
Dim Nuts As String

Private Sub Command1_Click()
Dim i As Integer
Nails = 0
Wood = 0
Screws = 0
Nuts = 0

For i = 0 To List1.ListCount - 1
TallyList List1.List(i)
Next i

MsgBox Nails & " Nails"
MsgBox Nuts & " Nuts"
MsgBox Wood & " Wood"
MsgBox Screws & " Screws"
MsgBox Bolts & " Bolts"
End Sub

Private Sub TallyList(Item As String)
If InStr(1, Item, "nail") <> 0 Then Nails = Nails + 1
If InStr(1, Item, "nut") <> 0 Then Nuts = Nuts + 1
If InStr(1, Item, "wood") <> 0 Then Wood = Wood + 1
If InStr(1, Item, "screw") <> 0 Then Screws = Screws + 1
If InStr(1, Item, "bolt") <> 0 Then Bolts = Bolts + 1
End Sub
LVL 28

Accepted Solution

vinnyd79 earned 300 total points
ID: 7069740
Actually you will need to check case,so you could change the TallyList sub in the above comment to:

Private Sub TallyList(Item As String)
Item = UCase(Item)
If InStr(1, Item, "NAIL") <> 0 Then Nails = Nails + 1
If InStr(1, Item, "NUT") <> 0 Then Nuts = Nuts + 1
If InStr(1, Item, "WOOD") <> 0 Then Wood = Wood + 1
If InStr(1, Item, "SCREW") <> 0 Then Screws = Screws + 1
If InStr(1, Item, "BOLT") <> 0 Then Bolts = Bolts + 1
End Sub
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.


Expert Comment

ID: 7069744
A category table would work much better.  You could then store the category ID in the listbox far to the right (out of sight) and use this to do your calculations, as well as other things.

1 = Screw
2 = Bolt
3 = Nut

In your listbox...

Wall Screw Alloy                1
1/4" Nut                        3

When you want to calculate cycle through all of your items in the listbox and count them

for x = 0 to list1.count - 1
     iCat = right(list1.listitems(x),1)
     'Now Count each category

Hope this helps

Expert Comment

ID: 7069747
vinnyd79, what if there are also some forks, fnifes and other stuff in the list? Adding more 'if's? :-)))
What if the types of items become 100 or more? :-))

Expert Comment

ID: 7069785
vinnyd79, your solution will work, but has limitations and requires more maintenance, just as Julian pointed out.

If you need to add a new category using a category list all you have to do is add a new category and the existing code works fine without modification.

Also, what is at some point in the future the descriptions of items change and the "Screw" or "Bolt" is left out of the desc?  Perhaps someone in the future does not know this is a requirement and does not put in the correct description?

I know this may never happen, but as developers we all know things happen that we just didn't count on :)
LVL 28

Expert Comment

ID: 7069820
True. Depending on the rest of the apps functionality it might not be a bad idea to use ADO.

Author Comment

ID: 7071746
We can assume the main category will always be present, so there will always be a Bolt, Screw, Fork but with some description added like Hex Bolt, Wood Screw, Plastic Fork.

Depending who is using the program it might be Hex Bolt, Hex Bolt 3/4, 1/2 Hex Bolt, 3mm Bolt.

If I expect to limit the users in their entries then it may become cumbersome to the user to have to remember how to enter this data.  I am thinking that vinnyd79 is probably the best way to handle this, although there are quite a few categories which would make my job in typing it all rather bulky.



Expert Comment

ID: 7072557
You could have a combobox with the categories and have them choose the category before entering the information.  All bolt types, doesn't matter what the description is, would go under the Bolt category.  There would only be one category for all bolts.  All you would have to do is initially assign categories to the existing descriptions.

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

600 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question