Solved

Vb 6 list box with two columns...

Posted on 2004-08-20
17
806 Views
Last Modified: 2007-12-19
lstSupply.AddItem "Book"
lstSupply.AddItem "Pen"
lstSupply.AddItem "Pencil"

above is working fine.

I want have a second column (hidden) to have SupplyID stored.

lstSupply.AddItem(0) 1:lstSupply(1).AddItem "Book"
lstSupply.AddItem(0) 1:lstSupply(1).AddItem "Pen"
lstSupply.AddItem(0) 1:lstSupply(1).AddItem "Pencil"

Q1.  What is correct code in vb 6 to deliver:

1   Book
2   Pen
3   Pencil

Q2. In case needed how can include the headers?  Like:  lstSupply.AddItem(0) "No.":lstSupply(1).AddItem "Supply"
Q3. How can I keep first column hidden?

Thanks,
0
Comment
Question by:Mike Eghtebas
  • 7
  • 5
  • 2
  • +3
17 Comments
 

Expert Comment

by:Imbue
ID: 11857894
Why do you want to have a hidden column? If you just want it to store information why not store the info elsewhere or have another listbox that is not visible?

If you really want a list box with columns and headers you should be using the listview control (in report mode) instead, it is part of Microsoft's common controls. Here is a link to it on MSDN http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon98/html/vbconusinglistviewcontrol.asp
0
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 11857931
If a user selects Book from:

1   Book
2   Pen
3   Pencil

I want to use its PK do to my work.  In Access I would do:  

Msgbox lstSupply(0)    'for 1   and
Msgbox lstSupply(1)    'for Book

I will check the link right now.

Mike


0
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 11858080
I need somthing similar to "user defined function" to do the job.  I have used it in Access.  I guess it should be the same.
0
 
LVL 14

Expert Comment

by:aelatik
ID: 11858150
You could use a 100 character long space seperator to store you ID's, see this sample :

Private Sub Form_Load()
lstSupply.AddItem "Book" & Space(100) & "1"
lstSupply.AddItem "Pen" & Space(100) & "2"
lstSupply.AddItem "Pencil" & Space(100) & "3"
End Sub

Private Sub lstSupply_Click()
MsgBox Split(lstSupply.Text, Space(100))(1)
End Sub


An other solution would be to place a hidden listbox containing the values.
You then could select the indexnumber of the first listbox to get the value from the second one.
0
 
LVL 1

Expert Comment

by:rickyman35
ID: 11859135
Dont use as list box add to your project a compoent called microsoft windows common controls 6.0 then add a list view to your project. It's so much better than a list box you can right click on it click properties and you get a bunch of easy options includeing headers. Your going to want to add this to your project though.

Private Sub Form_Load()
ListView1.View = lvwReport
End Sub

I dont think you can hide columns but you shoud just store that stuff as varaibles. Hope this works for you.
0
 
LVL 19

Expert Comment

by:Shauli
ID: 11859320
As rickyman35 mentioned, you need a ListView and not a List Box. With ListView you can add an item to the list, and add data to its key property. The key property must be unique for each item in the list, as in my example below:


ListView1.ListItems.Add Index, "Key", "Text", Icon, SmallIcon

Note that the properties are optional, and to omit, just add the relevant comma, so if you omit the index and the icon, for example, then it would look:

ListView1.ListItems.Add , "Key", "Text", , SmallIcon

S
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11859426

Just use the ItemData property of the listbox to store the index that you want.  Thats what its there for.

0
 
LVL 7

Accepted Solution

by:
Enlade earned 500 total points
ID: 11859440

For example,

lstSupply.Clear
lstSupply.AddItem "Book"
lstSupply.ItemData(lstSupply.ListCount - 1) = 1

lstSupply.AddItem "Pen"
lstSupply.ItemData(lstSupply.ListCount - 1) = 2

lstSupply.AddItem "Pencil"
lstSupply.ItemData(lstSupply.ListCount - 1) = 3


Then just to test it add the Click event like this:

Private Sub lstSupply_Click()
  MsgBox lstSupply.ItemData(lstSupply.ListIndex)
End Sub


0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 7

Expert Comment

by:Enlade
ID: 11859450

I mean, ItemData is really used to store the Primary Key from the database table you are getting the data from.  If there are multiple keys in the primary key then it is probably still better to merge them and use them in the ItemData rather then add them as hidden columns of the table (just parse the ItemData value to get each keys value).  The point is that very often there is just one key in the primary key and it is oftem just a number.  In those cases it just make more sense to use ItemData then to try to put it into a hidden column.  At least thats my story.

0
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 11859529
Imbue (via a link),  rickyman35, and Shauli are recommending ListView (Shauli with more detail).  I am conveinced ListView is the way to go.  But, because of time crunch, I will be using  aelatik's quick-and-dirty solution (aelatik, I hope you don't mind me calling it that).  

Later on, I will practice on ListView and replace all list boxes in my projrct with it.  Now,  I am accepting aelatik's solution because I am using it; although ListView is a better solution.

FYI:  Having a hidden column of some sort helps me to have PK value of the item next by ItemDesc.  This way, when a selection is made, I have PK right next to it using (for now):  Split(lstSupply.Text, Space(100))(1)

I am expectin ListView to do the samething when I start using it.

Regards,

Mike
0
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 11859560
Enlade,

I just saw your latest comment. brb

Mike
0
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 11859735
Enlade,,

That did it.  Sorry for not given it a try.

Mike
0
 
LVL 19

Expert Comment

by:Shauli
ID: 11859822
Mike,

You have chosen to go with itemdata, which is fine. Nevertheless, List box is very limited with its options, comparing to the listview component. The itemdata is a numeric value, which means that only numbers can be stored in it. ListView, anbles you unlimited number of columns, visible or hidden, just by setting its View property to report. It allows a key which can be of either alpha or numeric, or a combination of both for each item. It allows to add a small icon to the left of the row. It allows an easy manipulation of each item and row. It allows editing the item text, and much more. It is by far much more advanced than the listbox. It worth checking :)

S
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11860418

Shauli is correct about the ItemData only taking numaric values.  For some reason I was remembering something I did using the Tag property and parsing it (unrelated idea).  Still, ItemData is quite useful for whatever control you end up using.  Very often you are working with tables that have only one part to the primery key and often that part is a unique numaric value (like the Auto Increment identifiers in Access or whatever), so its a pretty useful property at times.

As far as which control you should use.  That is up to you.  Personally I try to use thrid party controls over the built in controls unless its some simple program.  Like for instance, for some simple applications I might use:

http://www.fpoint.com/products/list/list.aspx

Or any of the other thrid party list controls that I have in my toolbox.  But no matter which I choose I still often make use of the ItemData property.
0
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 11861905
Hi  Shauli,

I am sure listview is the eay to go.  But I need to familiarize myself with it first and don't have the time for delivery is very near.  I will rewrite the code later to use listview.

Regards,

Mike
0
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 11932651
FYI:

lstSupply.AddItehm rs("Supply")        ' This line worked fine.  It adds supply names to lstSupply using tblSupply.  
                                                      ' This table has filed SupplyID and it is used in the next line.

lstSupply.ItemData(lstSupply.ListCount-1) = rs("SupplyID")      

Msgbox lstSupply.ItemData(lstSupply.ListCount-1)        ' This line gave me correct SupplyID for the first item in the list (tblSupply).  But, It gave 0 for the rest of items.

This is because "Select * From tblSupply Order By Supply" didn't had SupplyID filed in ascending order.  When I changed it just to "Select * From tblSupply", it worked fine (default sort is by suppltID).

Mike
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11991213

I don't think that it matters what order you add items to the listbox.  So, I'm not sure why the "select" order would have anything to do with the values you are assigning to ItemData.  I mean, I could have just as easily done this:

lstSupply.Clear
lstSupply.AddItem "Book"
lstSupply.ItemData(lstSupply.ListCount - 1) = 25

lstSupply.AddItem "Pen"
lstSupply.ItemData(lstSupply.ListCount - 1) = 5

lstSupply.AddItem "Pencil"
lstSupply.ItemData(lstSupply.ListCount - 1) = 10

And that would work just fine even though the ItemData isn't being assigned a number in any sequential ordering.  So, maybe you have the Sorted property of your listbox set to "True" or something like that.  Using the Sorted property in conjunction with ItemData can be a problem unless you are careful.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

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

13 Experts available now in Live!

Get 1:1 Help Now