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.
Printable View
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.
Create a recordset pulling back the records that you want using an SQL SELECT query, then run something like thisCode: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
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?
Been doin' it for about 14 years. :)Quote:
Originally Posted by Justin M
Lets start from the beginning. From your VB6 project, do you have a connection to your database?
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
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.
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.
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.