Hello everyone!!! I'm a new poster here. Can somebody help me with my project. My listview is not working. :( I need to search a person's name using his/her lastname and firstname and display all the people with the same lastname or firstname and those data will display in the listview. the problem is, my listview is not working. it says something like this "invalid property value" and higlighten a part of my code (lst.SubItems(1) = RS!Firstname). here is my code. I hope you can help me. :'(

Private Sub txtsearch_KeyDown(KeyCode As Integer, Shift As Integer)
Dim str As String
Dim lst As ListItem
If KeyCode = 13 Then
Set RS = New Recordset
str = "select * from tblrecords where Lastname+' '+Firstname like '" & txtsearch.Text & "'"
RS.Open str, cn, adOpenDynamic, adLockOptimistic
lsvwed.ListItems.Clear
If RS.RecordCount > 0 Then
    RS.MoveFirst
    While Not RS.EOF()
        With lsvwed
            Set lst = .ListItems.Add(, , (RS!Lastname))
            lst.SubItems(1) = RS!Firstname
            lst.SubItems(2) = RS!Middlename
            lst.SubItems(3) = RS!Address
            lst.SubItems(4) = RS!Age
         
        End With
        RS.MoveNext
    Wend
End If

If RS.EOF Then
MsgBox "File Not Found!", vbExclamation
If RS.State = adStateOpen Then RS.Close
Set RS = Nothing
End If
End If
End Sub

Recommended Answers

All 5 Replies

Try this:

With lsvwed

            Set lst = .ListItems.Add(, , (RS!Lastname))
            lst.(lsvwed.ListItems.Count).SubItems(1) = RS!Firstname
            lst.(lsvwed.ListItems.Count).SubItems(2) = RS!Middlename
            lst.(lsvwed.ListItems.Count).SubItems(3) = RS!Address
            lst.(lsvwed.ListItems.Count).SubItems(4) = RS!Age
         
        End With

sir the code that you gave didn't work. all of it turns red. I think the wrong one is in the .SubItems(). I'm not sure. :)

Can you check your listview control columns if it corresponds to the number of your subitems.?

I think it needs 5 columns.

commented: Thank you for helping us fight spam! +16

although i cam elate but try this
check in ur database the column Firstname
this what is giving u the error bec the value cant be displayed
any way declaring a function and calling it make your work much easier for example u can make ur code like this
Private Sub txtsearch_KeyDown(KeyCode As Integer, Shift As Integer)
Dim str As String
Dim lst As ListItem
If KeyCode = 13 Then
Set RS = New Recordset
str = "select * from tblrecords where Lastname+' '+Firstname like '" & txtsearch.Text & "'"
RS.Open str, cn, adOpenDynamic, adLockOptimistic
lsvwed.ListItems.Clear
call loadlsvwed()
End If
End Sub

then u declare the public sub

public sub loadlsvwed()
on error resume next
with lsvwed
.view = lvwreport
.fullrowselect + true
.multiselect + true
.columnheaders.clear
.columnsheaders.add, , "Lastname", 1500
.columnsheaders.add, , "Firstname", 1500
.columnsheaders.add, , "Middlename", 1500
.columnsheaders.add, , "Address", 3000
.columnsheaders.add, , "Age", 500
.listitems.clear
do while not rs.EOF
set str = .listitems.add(, , rs.Fields("Lastname"), 0, 0)
.subitems(1) = rs.Fields("Firstname")
.subitems(2) = rs.Fields("Middlename")
.subitems(3) = rs.Fields("Address")
.subitems(4) = rs.Fields("Age")
rs.movenext
loop
end with
end sub

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.