Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multi-column List Box 4

Status
Not open for further replies.

rickyoswald

Programmer
Jun 12, 2004
59
GB
I have been trying to make a multi-column list box which will fill with data from a database. The default listbox control in VB6 only has one column, is there property or a similar control that I can use?
 
I found this snippit on the net.

Code:
Option Explicit
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset


Private Sub Command1_Click()
 End
End Sub

' Multicolumn listbox or combo
' Courier font is used because it has an equal character width
' unlike other fonts

Private Sub Form_Load()
 Dim x As Integer
 Dim y As Integer
 Set cn = New ADODB.Connection
 Set rs = New ADODB.Recordset
 
 cn.Provider = "Microsoft.Jet.OLEDB.4.0"
 cn.ConnectionString = App.Path & "\sample.mdb"
 cn.Open
 
 rs.Source = "select * from suppliers"
 rs.Open , cn, adOpenKeyset, adLockOptimistic
 
 Do Until rs.EOF
   x = Len(Left(rs!CompanyName, 38))
   x = 38 - x
   y = Len(Left(rs!ContactName, 20))
   y = 22 - y
   List1.AddItem rs!CompanyName & Space(x) & Space(2) & Left(rs!ContactName, 20) & Space(y) & rs!contacttitle
   rs.MoveNext
 Loop

 rs.Close
 cn.Close
 Set rs = Nothing
 Set cn = Nothing

End Sub

Hope it helps. :)

Wraygun

***You can't change your past, but you can change your future***
 
Have you considered the Flexgrid?
 
Or the ListView Control??? It is Microsoft Windows Common Controls.
 
The ListView Control is possible what you are elooking for.

Transcend
[gorgeous]
 
ListView? I looked through the references but found no Microsoft Windows Common Controls.
 
It is a control, add it from the toolbar

Microsoft Windows Common Controls 6.0
 
There is no ListView control! I also looked in the References for "Microsoft Windows Common Controls 6.0" which does not exist. I have service pack 5 and other bits I had to download to get my database to work. Don't tell me this is another thing I have to download?!
 
also what is "rs.Source = "select * from suppliers"" for? I have never used this and my database works fine.
 
Go to the Projects menu and do Controls... you will find Microsoft Windows Common Controls 6.0 there. It is not in References because it is a CONTROL!
 
It won't be under controls, because that won't exist.

It WILL however be under components.
 
yeah, that's what I mean... sorry for the confusion... and thanks for pointing that out mmilian...
 
If you go into the custom property of the listview you can add and remove coplumns, you can also add and remove at runtime. change the view property to report, not icon.

Matt
 
I used the code that Wraygun provided, it worked fine but wasn't really what I wanted. I have the ListView working now (multiple columns!). As usual, thanks for all your help. I have to go link it to a database and populate it now ;)
 
Okay I have been playing with the for a while now. How exactly do I add items to the columns? I assume it would be control.column.additem or somthing similar?
 
There are TONS of examples on the listview control in this forum. below is an example written on the fly so if it breaks sorry, but debugging should be simple.

Code:
Form1.lstVw1.ListItems.Add 1, , "This"
Form1.lstVw1.ListItems(1).SubItems(1) = "is"
Form1.lstVw1.ListItems(1).SubItems(2) = "adding"
Form1.lstVw1.ListItems(1).SubItems(3) = "items"
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top