Results 1 to 1 of 1

Thread: Database - How can I fill a ListView with values in a database?

  1. #1

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

    Database - How can I fill a ListView with values in a database?

    The following code assumes you are already using ADODB code, and have a connection to the database.

    If you do not have the above, please see the attachment in this thread for help.

    First, you must add a ListView control to your project. The ListView control is found in Microsoft Windows Common Controls 6.0 (SPx) (where x = your VB service pack number). To add these controls, click Project/Components from the VB IDE. Locate the above mentioned controls and check the cooresponding checkbox and click OK.

    Add a ListView control to your form, and draw it out to the desired dimensions.

    Next, right click on the ListView and click Properties.

    The ListView control has 4 different "View" possibilities:
    1. lvwIcon
    2. lvwSmallIcon
    3. lvwList
    4. lvwReport

    This example will be using lvwReport. Change the view to reflect this.

    Next, click on the ColumnHeaders tab.

    This example will be creating a recordset of employee information containing:
    First Name, Last Name, Date Of Birth, Employee Number and Date Of Hire

    Create column headers with these titles.

    The database fields we will be using are:
    FName, LName, DOB, EmpNumb, DOH and the name of the table is tblEmployeeInfo

    The database fields we will be using are:

    FName, LName, DOB, EmpNumb, DOH and the name of the table is tblEmployeeInfo
    vb Code:
    1. Dim strSQL As String 'our query string
    2. Dim oRS as ADODB.Recordset 'our recordset object
    3. Dim lvwItem As ListItem 'this is necessary to directly reference the ListView control
    4. Set oRS = New ADODB.Recordset
    5.  
    6. 'change this SQL as appropriate for your needs
    7. strSQL = "SELECT FName, LName, DOB, EmpNumb, DOH FROM tblemployeeinfo "
    8. 'change oConn to reflect the Connection object you are using in your program
    9. oRs.Open strSQL, oConn, adOpenForwardOnly, adLockReadOnly
    10.  
    11. 'load the listview
    12. Do While Not oRS.EOF
    13.    Set lvwItem = ListView1.ListItems.Add(, , oRS.Fields.Item("FName").Value)
    14.    lvwItem.SubItems(1) = oRS.Fields.Item("LName").Value
    15.    'change the date format as required
    16.    lvwItem.SubItems(2) = Format(oRS.Fields.Item("DOB").Value, "mm/dd/yyyy")
    17.    lvwItem.SubItems(3) = oRS.Fields.Item("EmpNumb").Value
    18.    'change the date format as required
    19.    lvwItem.SubItems(4) = Format(oRS.Fields.Item("DOH").Value, "mm/dd/yyyy")
    20.    oRS.MoveNext
    21. Loop
    22. oRS.Close
    23. Set oRS = Nothing
    Last edited by Hack; Apr 3rd, 2007 at 09:47 AM.

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