|
-
Feb 12th, 2008, 02:18 PM
#1
Thread Starter
PowerPoster
How to load a listbox to a listview1
Hey guys i have a database called database1, and that DB has 3 columns in the table1, can anyone tell me how I could load that DB into a listview1 that also has 3 columns? thank you.
-
Feb 12th, 2008, 02:22 PM
#2
Re: How to load a listbox to a listview1
Create a recordset pulling back the records that you want using an SQL SELECT query, then run something like this
Code:
Dim MyItem As ListItem
Do While Not AdoRs.EOF
Set MyItem = ListView1.ListItems.Add(, , AdoRs(0))
MyItem.SubItems(1) = AdoRs(1)
MyItem.SubItems(2) = AdoRs(2)
AdoRs.MoveNext
Loop
-
Feb 12th, 2008, 02:29 PM
#3
Thread Starter
PowerPoster
Re: How to load a listbox to a listview1
When you say pull back records what would that mean?
I want all of the database loaded it only has 3 columns just like the listivew1. You can use SQL in VB 6?
-
Feb 12th, 2008, 02:39 PM
#4
Re: How to load a listbox to a listview1
 Originally Posted by Justin M
You can use SQL in VB 6?
Been doin' it for about 14 years. 
Lets start from the beginning. From your VB6 project, do you have a connection to your database?
-
Feb 12th, 2008, 02:43 PM
#5
Thread Starter
PowerPoster
Re: How to load a listbox to a listview1
Not yet I do not. Btu for an ex I used a database for a listbox I used this ..
VB Code:
Dim con As ADODB.Connection Dim strSQL As String Dim strCol As String Set con = New ADODB.Connection con.CursorLocation = adUseClient con.Open "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.path & "\DatabaseMain1.mdb;" _ & "Jet OLEDB:Database Password=*****;" strSQL = "SELECT * FROM table1" strCol = "viruslist" Call FillCombo(List2, con, strSQL, strCol) con.Close Set con = Nothing
-
Feb 12th, 2008, 02:43 PM
#6
Re: How to load a listbox to a listview1
Actually, take a look at out Database FAQ section.
I'm pretty sure everything you need to know about connecting to your database and running queries is covered there.
-
Feb 12th, 2008, 02:44 PM
#7
Re: How to load a listbox to a listview1
That looks like you have a connection.
And, it looks like you are running a SELECT query.
So, just take the ListView code I posted, use the recordset you created, and you should be in business.
-
Feb 12th, 2008, 03:05 PM
#8
Thread Starter
PowerPoster
Re: How to load a listbox to a listview1
Ok but I get an error with my filll combo code. ..
VB Code:
Public Sub FillCombo(objComboBox As ListBox, _
oConn As ADODB.Connection, _
strSQL As String, _
strFieldToShow As String, _
Optional strFieldForItemData As String)
Dim oRS As ADODB.Recordset 'Load the data
Set oRS = New ADODB.Recordset
oRS.Open strSQL, oConn, adOpenForwardOnly, adLockReadOnly, adCmdText
With objComboBox 'Fill the combo box
.Clear
If strFieldForItemData = "" Then
Do While Not oRS.EOF '(without ItemData)
.AddItem oRS.Fields(strFieldToShow).Value
oRS.MoveNext
Loop
Else
Do While Not oRS.EOF '(with ItemData)
.AddItem oRS.Fields(strFieldToShow).Value
.ItemData(.NewIndex) = oRS.Fields(strFieldForItemData).Value
oRS.MoveNext
Loop
End If
End With
oRS.Close 'Tidy up
Set oRS = Nothing
End Sub
Right now its set to work with a listbox, but how would it work with a listview with 3 columns?
Edit: I also get a variable not defined with the code you posted, it is saying AdoRs is not defined???
And with my code ...
VB Code:
#
Call FillCombo(List2, con, strSQL, strCol)
#
con.Close
#
Set con = Nothing
That talks to list2, but how would it be able to talk to listview1 and the 3 columns there.
Last edited by Justin M; Feb 12th, 2008 at 03:23 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|