-
Apr 3rd, 2007, 09:09 AM
#1
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:
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
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|