Results 1 to 8 of 8

Thread: How to load a listbox to a listview1

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    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.

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    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

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    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?

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: How to load a listbox to a listview1

    Quote 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?

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    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:
    1. Dim con As ADODB.Connection
    2. Dim strSQL As String
    3. Dim strCol As String
    4. Set con = New ADODB.Connection
    5. con.CursorLocation = adUseClient
    6. con.Open "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.path & "\DatabaseMain1.mdb;" _
    7.     & "Jet OLEDB:Database Password=*****;"
    8. strSQL = "SELECT * FROM table1"
    9. strCol = "viruslist"
    10. Call FillCombo(List2, con, strSQL, strCol)
    11. con.Close
    12. Set con = Nothing

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    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.

  7. #7
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    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.

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    Re: How to load a listbox to a listview1

    Ok but I get an error with my filll combo code. ..
    VB Code:
    1. Public Sub FillCombo(objComboBox As ListBox, _
    2.                         oConn As ADODB.Connection, _
    3.                         strSQL As String, _
    4.                         strFieldToShow As String, _
    5.                         Optional strFieldForItemData As String)
    6.  
    7.             Dim oRS As ADODB.Recordset  'Load the data
    8.               Set oRS = New ADODB.Recordset
    9.               oRS.Open strSQL, oConn, adOpenForwardOnly, adLockReadOnly, adCmdText
    10.               With objComboBox          'Fill the combo box
    11.                 .Clear
    12.                 If strFieldForItemData = "" Then
    13.                   Do While Not oRS.EOF      '(without ItemData)
    14.                     .AddItem oRS.Fields(strFieldToShow).Value
    15.                   oRS.MoveNext
    16.                   Loop
    17.                 Else
    18.                   Do While Not oRS.EOF      '(with ItemData)
    19.                     .AddItem oRS.Fields(strFieldToShow).Value
    20.  
    21.                     .ItemData(.NewIndex) = oRS.Fields(strFieldForItemData).Value
    22.              oRS.MoveNext
    23.                   Loop
    24.                 End If
    25.               End With
    26.               oRS.Close                 'Tidy up
    27.               Set oRS = Nothing
    28.           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:
    1. #
    2. Call FillCombo(List2, con, strSQL, strCol)
    3. #
    4. con.Close
    5. #
    6. 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
  •  



Click Here to Expand Forum to Full Width