Listbox sorting and comparing and tally

Posted on 2002-06-11
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 100 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses
Course of the Month6 days, 9 hours left to enroll

636 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