Click here to Skip to main content
15,886,851 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can I take a row of data from a database and add it to my listview?

I am looking to add lets say "Name" "age" and "sex" from different tables on my database all in the same row so that it will show up in my listview in that order in my header columns?


any advise would be greatly appreciated or a better method of how to accomplish this.


thank you in advance!! :)
Posted

1 solution

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


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

Dim strSQL As String 'our query string
Dim oRS as ADODB.Recordset 'our recordset object
Dim lvwItem As ListItem 'this is necessary to directly reference the ListView control
Set oRS = New ADODB.Recordset

'change this SQL as appropriate for your needs
strSQL = "SELECT FName, LName, DOB, EmpNumb, DOH FROM tblemployeeinfo "
'change oConn to reflect the Connection object you are using in your program
oRs.Open strSQL, oConn, adOpenForwardOnly, adLockReadOnly

'load the listview
Do While Not oRS.EOF
Set lvwItem = ListView1.ListItems.Add(, , oRS.Fields.Item("FName").Value)
lvwItem.SubItems(1) = oRS.Fields.Item("LName").Value
'change the date format as required
lvwItem.SubItems(2) = Format(oRS.Fields.Item("DOB").Value, "mm/dd/yyyy")
lvwItem.SubItems(3) = oRS.Fields.Item("EmpNumb").Value
'change the date format as required
lvwItem.SubItems(4) = Format(oRS.Fields.Item("DOH").Value, "mm/dd/yyyy")
oRS.MoveNext
Loop
oRS.Close
Set oRS = Nothing
 
Share this answer
 
Comments
Dale 2012 4-Mar-12 5:24am    
your example is great but would it be similar to how access database works? I cannot seem to figure out ADODB.recordset.

please help

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900