Solved

Listbox sorting and comparing and tally

Posted on 2002-06-11
9
213 Views
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.
0
Comment
Question by:alariac
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 5

Expert Comment

by:Julian_K
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

by:vinnyd79
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

by:
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
0
 
LVL 4

Expert Comment

by:gencross
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
next

Hope this helps
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 5

Expert Comment

by:Julian_K
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

by:gencross
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

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

Author Comment

by:alariac
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

by:gencross
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

708 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now