Solved

# Listbox sorting and comparing and tally

Posted on 2002-06-11
Medium Priority
230 Views
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.

0
Question by:alariac
• 3
• 3
• 2
• +1

LVL 5

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.

0

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
0

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
0

LVL 4

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

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
next

Hope this helps
0

LVL 5

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? :-))
0

LVL 4

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 :)
0

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.
0

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.

Hmm...

0

LVL 4

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.
0

## Featured Post

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.