Write a program to use listview control in Visual Basic 6.0 and explain with example, properties, functions and features in it.
Listview in VB 6.0:
Listview control is one of the most important feature available in VB 6.0. It plays a vital role in creating applications with grid support. It is primarily used to display data in grid in the form of columns and rows. Each data and field is properly managed in this control. Also it is very easy to use and implement in code.
How to Add Listbox Control in VB 6.0?
Project >> Components… (Ctrl + T) >> Tick Microsoft Windows Common Controls 6.0 (SP6) >> OK
It will add library related with it. In this you will find control ‘ListView’ at left hand side panel i.e. in ToolBox.

Fig. : ListView Icon in ToolBox in VB 6.0
Simply Double click on it, It will paste on your Form designer. Add following source code inside form and check its performance.
For Database Access use ,
Add References : Microsoft ActiveX Data Objects 2.0 Library as
Project >> References… >> Microsoft ActiveX Data Objects 2.0 Library for Data Access
Source Code:
Dim ch As String
Dim Con As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim cmd As New ADODB.Command
Private Sub cmdCancel_Click()
Call clearall
cmdSave.Caption = "&Save"
End Sub
Private Sub cmdDelete_Click()
If Len(Trim(txtRno.Text)) <> 0 Then
Set rs = New ADODB.Recordset
rs.Open "Delete from Contact_Master where Rno=" & txtRno.Text, Con, 1, 2
MsgBox "Record deleted successfully !", vbInformation, "Deletion Successful"
Call Filllist
Call clearall
cmdSave.Caption = "&Save"
End If
End Sub
Private Sub cmdNew_Click()
Call clearall
txtRno.Enabled = True
txtRno.SetFocus
ch = "new"
cmdSave.Caption = "&Save"
End Sub
Private Sub cmdSave_Click()
If ch = "new" And cmdSave.Caption = "&Save" Then
Set rs = New ADODB.Recordset
rs.Open "Select * from Contact_Master", Con, 1, 2
rs.AddNew
Else
rs.Open "Select * from Contact_Master where Rno=" & txtRno.Text, Con, 1, 2
cmdSave.Caption = "&Save"
End If
rs!Rno = Val(txtRno.Text)
rs!Nm = txtName.Text
rs!Mob = txtContact.Text
rs.Update
rs.Close
Set rs = Nothing
Call Filllist
Call clearall
cmdNew.SetFocus
End Sub
Private Sub Form_Load()
Call openConnection
Call initlist
Call Filllist
End Sub
Public Sub openConnection()
Set Con = New ADODB.Connection
If Con.State = 1 Then
Con.Close
End If
Con.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "DatabaseContacts.mdb;"
Con.CursorLocation = adUseClient
End Sub
Public Sub clearall()
txtName.Text = ""
txtRno.Text = ""
txtContact.Text = ""
End Sub
Public Sub initlist() ' Used to Inialize the properties of ListView
With LstData
.View = lvwReport
.Appearance = ccFlat
.FullRowSelect = True
.GridLines = False
.HotTracking = True
.HoverSelection = True
.HideSelection = False
.ColumnHeaders.Add 1, "d1", "Roll Number"
.ColumnHeaders.Add 2, "d2", "Name"
.ColumnHeaders.Add 3, "d3", "Contact"
End With
End Sub
Public Sub Filllist() ' Fills data from database to listview
Set rs = New ADODB.Recordset
With rs
.Open "Select * from Contact_master order by Rno", Con, 1, 2
If .RecordCount > 0 Then
LstData.ListItems.Clear
.MoveFirst
Do While rs.EOF = False
Set Objlist = LstData.ListItems.Add(, , !Rno) ' Adds Items to List
Objlist.SubItems(1) = !Nm
Objlist.SubItems(2) = !Mob
.MoveNext
Loop
Else
LstData.ListItems.Clear
End If
.Close
Set rs = Nothing
End With
End Sub
Private Sub LstData_Click() ' ListView Click Event used to perform modification
txtRno.Enabled = False
If LstData.ListItems.Count = 0 Then ' ListItems Count
MsgBox "Sorry ! Records not found !!", vbInformation, "Records Not Found"
Exit Sub
End If
If LstData.ListItems.Count > 0 Then
txtRno.Text = LstData.ListItems(LstData.SelectedItem.Index).Text
txtName.Text = LstData.ListItems(LstData.SelectedItem.Index).ListSubItems(1).Text
txtContact.Text = LstData.ListItems(LstData.SelectedItem.Index).ListSubItems(2).Text
End If
cmdSave.Caption = "&Modify"
End Sub
Output:



i am creating a project and my project requirement search data how can search data in mdb file
and display all data and using textbox to add name and search this name and all field are fill automatically
and i am using a adodc connection
plz. help me
use for loop which checks each and every record in database. Check records one by one and if exact match found then you will get what you want.
OR
Use simple sql query to search data as
“select * from {table_name} where {col_nm}=’” & text1.text & “‘”… and fill the data in textbox
We shall mail you all above codes in next 5 hours. Thanks for visiting technoexam.com
pls help..
im making a project in vb 6.0
could u help me on how to save the all the data in listview to database using access..
thanx…
Please refer following functions in above program as well as save button code as,
Public Sub initlist() ‘ Used to Inialize the properties of ListView
With LstData
.View = lvwReport
.Appearance = ccFlat
.FullRowSelect = True
.GridLines = False
.HotTracking = True
.HoverSelection = True
.HideSelection = False
.ColumnHeaders.Add 1, “d1″, “Roll Number”
.ColumnHeaders.Add 2, “d2″, “Name”
.ColumnHeaders.Add 3, “d3″, “Contact”
End With
End Sub
Public Sub Filllist() ‘ Fills data from database to listview
Set rs = New ADODB.Recordset
With rs
.Open “Select * from Contact_master order by Rno”, Con, 1, 2
If .RecordCount > 0 Then
LstData.ListItems.Clear
.MoveFirst
Do While rs.EOF = False
Set Objlist = LstData.ListItems.Add(, , !Rno) ‘ Adds Items to List
Objlist.SubItems(1) = !Nm
Objlist.SubItems(2) = !Mob
.MoveNext
Loop
Else
LstData.ListItems.Clear
End If
.Close
Set rs = Nothing
End With
End Sub
what is objlist in this vb code
pl.send
Just we had set “LstData.ListItems.Add(, , !Rno)” to objlist to ease of use. It is set of object list.. If we not used objlist then we had to use lstData each and every time for each record access.
There is error variable not defined and high light objlist pl.solve this problem
I send my testing project your emal
thank you
saman
Hello Saman ! We had just solved your problem and sent new project with description of errors occurred for ease of error understanding for you. Please, check your E-Mail and if any problem, queries then don’t hesitate to contact us..
Thanks for contacting and commenting…!:)